吴裕雄--python学习笔记:爬虫
import chardet
import urllib.request page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网页
htmlCode = page.read() #获取网页源代码 print(chardet.detect(htmlCode)) #打印返回网页的编码方式
{'encoding': 'utf-8', 'confidence': 0.99, 'language': ''}
data = htmlCode.decode('utf-8')
print(data) #打印网页源代码
pageFile = open('E:\\pageCode.txt','wb')#以写的方式打开pageCode.txt
pageFile.write(htmlCode)#写入
pageFile.close()#开了记得关
获取其他信息 打开pageCode.txt文件(也可以直接在原网页F12调试获取),查看需要获取数据的标签信息。 比如我现在要拿图片 写出图片的正则表达式: reg = r'src="(.+?\.jpg)"' 解释下吧——匹配以src="开头然后接一个或多个任意字符(非贪婪),以.jpg" 结尾的字符串。比如图中红框内src后 双引号里的链接就是一个匹配的字符串。 接着我们要做的就是从get_html方法返回的辣么长一串字符串中 拿到 满足正则表达式的 字符串。 用到python中的re库中的 re.findall(str) 它返回一个满足匹配的字符串组成的列表
import re
import chardet
import urllib.request page = urllib.request.urlopen('http://www.meituba.com/tag/juesemeinv.html') #打开网页
htmlCode = page.read() #获取网页源代码 #print(chardet.detect(htmlCode)) #查看编码方式
data = htmlCode.decode('utf-8')
#print(data) #打印网页源代码 #pageFile = open('pageCode.txt','wb')#以写的方式打开pageCode.txt
#pageFile.write(htmlCode)#写入
#pageFile.close()#开了记得关 reg = r'src="(.+?\.jpg)"'#正则表达式
reg_img = re.compile(reg)#编译一下,运行更快
imglist = reg_img.findall(data)#进行匹配
for img in imglist:
print(img)
http://ppic.meituba.com:83/uploads3/181201/3-1Q20111553V11.jpg
http://ppic.meituba.com:83/uploads2/180622/3-1P62215532D61.jpg
http://ppic.meituba.com:83/uploads2/180605/3-1P6051000144I.jpg
http://ppic.meituba.com:83/uploads2/170511/8-1F5110URc35.jpg
http://ppic.meituba.com:83/uploads/160322/8-1603220U50O23.jpg
http://ppic.meituba.com:83/uploads2/180317/3-1P31F91U1X9.jpg
http://ppic.meituba.com:83/uploads/160718/7-160GQ51G0b4.jpg
http://ppic.meituba.com:83/uploads2/170517/8-1F51G50301Q3.jpg
http://ppic.meituba.com:83/uploads/161010/7-1610101A202B0.jpg
http://ppic.meituba.com:83/uploads2/171102/7-1G102093511F7.jpg
http://ppic.meituba.com:83/uploads2/170901/7-1FZ1100545438.jpg
http://ppic.meituba.com:83/uploads/160625/8-160625093044631.jpg
http://ppic.meituba.com:83/uploads/160419/7-160419161553153.jpg
http://ppic.meituba.com:83/uploads2/170323/7-1F323103404A2.jpg
http://ppic.meituba.com:83/uploads2/170322/7-1F322105R1255.jpg
http://ppic.meituba.com:83/uploads2/170211/7-1F21110040Y63.jpg
http://ppic.meituba.com:83/uploads2/170110/7-1F110102005930.jpg
http://ppic.meituba.com:83/uploads/160618/8-16061Q04450391.jpg
http://ppic.meituba.com:83/uploads2/170330/3-1F3301HI6138.jpg
http://ppic.meituba.com:83/uploads2/161230/4-161230100U5V8.jpg
然后将图片下载到本地 urllib库中有一个 urllib.request.urlretrieve(链接,名字) 方法,它的作用是以第二个参数为名字下载链接中的内容,我们来试用一下
x = 0
for img in imglist:
print(img)
urllib.request.urlretrieve('http://ppic.meituba.com/uploads/160322/8-1603220U50O23.jpg', '%s.jpg' % x)
x += 1
import re
import urllib.request def getGtmlCode():
html = urllib.request.urlopen("http://www.quanshuwang.com/book/44/44683").read() #获取网页源代码
html = html.decode("gbk") #转成该网站格式
reg = r'<li><a href="(.*?)" title=".*?">(.*?)</a></li>' #根据网站样式匹配的正则:(.*?)可以匹配所有东西,加括号为我们需要的
reg = re.compile(reg)
urls = re.findall(reg, html)
for url in urls:
#print(url)
chapter_url = url[0] #章节路径
chapter_title = url[1] #章节名
chapter_html = urllib.request.urlopen(chapter_url).read() #获取该章节的全文代码
chapter_html = chapter_html.decode("gbk")
chapter_reg = r'</script> .*?<br />(.*?)<script type="text/javascript">' #匹配文章内容
chapter_reg = re.compile(chapter_reg,re.S)
chapter_content = re.findall(chapter_reg, chapter_html)
for content in chapter_content:
content = content.replace(" ","") #使用空格代替
content = content.replace("<br />","") #使用空格代替
print(content)
f = open('E:\\aa\\{}.txt'.format(chapter_title),'w') #保存到本地
f.write(content) getGtmlCode()
吴裕雄--python学习笔记:爬虫的更多相关文章
- 吴裕雄--python学习笔记:爬虫基础
一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...
- 吴裕雄--python学习笔记:爬虫包的更换
python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...
- 吴裕雄--python学习笔记:sqlite3 模块
1 sqlite3.connect(database [,timeout ,other optional arguments]) 该 API 打开一个到 SQLite 数据库文件 database 的 ...
- 吴裕雄--python学习笔记:os模块函数
os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'. os.getcwd:得 ...
- 吴裕雄--python学习笔记:os模块的使用
在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径下 ...
- 吴裕雄--python学习笔记:BeautifulSoup模块
import re import requests from bs4 import BeautifulSoup req_obj = requests.get('https://www.baidu.co ...
- 吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理
import sqlite3 conn = sqlite3.connect('E:\\student.db') print("Opened database successfully&quo ...
- 吴裕雄--python学习笔记:sqlite3 模块的使用与学生信息管理系统
import sqlite3 cx = sqlite3.connect('E:\\student3.db') cx.execute( '''CREATE TABLE StudentTable( ID ...
- python学习笔记——爬虫学习中的重要库urllib
1 urllib概述 1.1 urllib库中的模块类型 urllib是python内置的http请求库 其提供了如下功能: (1)error 异常处理模块 (2)parse url解析模块 (3)r ...
随机推荐
- 绩效软件交流-ZQDJ
积分制(主管激励下属)短期任务积分 长期任务积分 制度积分 固定积分任务工作项 评估表 ,取中间值工时调整 工作表现 创新加分 难度加分 贡献加分 绩效分-积分(软件亮点) 分开做 没有管理员的中层 ...
- IntelliJ IDEA 2019.2 LUA环境搭建说明
1.搭建GCC 添加系统环境变量PATH 为C:\MinGW\bin目录 测试命令进入CMD gcc -v 2.编译LUA cd到lua/src目录 mingw32-make min ...
- 独立t检验
方差相同个数相同的独立t检验 5.某饲料厂要比较A.B两种配合饲料在养猪生产中的效果,选取12头情况相似的猪,随机分成两组,分别饲喂两种配合饲料,其60天增重(单位:kg)见下表. 饲 料 60d增重 ...
- day52-线程-队列
#1.线程的队列是使用import queue,如果使用from threading import Queue会报错,因为threading模块没有Queue. #也就是说,线程队列Queue是在qu ...
- Uber坚持不盈利,葫芦里到底卖的是什么药?
近日,据媒体报道在美国科罗拉多州阿斯彭举办的<财富>科技头脑风暴大会上,Uber CEO达拉·科斯罗萨西表示,Uber无需在2019年下半年上市计划实施前保持盈利状态. 首先要明确一点的是 ...
- video文件转blob
//创建 XMLHttpRequest 对象 var xhr = new XMLHttpRequest(); //配置请求方式.请求地址以及是否同步 xhr.open('POST', './play' ...
- UI自动化(selenium+python)之元素定位的三种等待方式
前言 在UI自动化过程中,常遇到元素未找到,代码报错的情况.这种情况下,需要用等待wait. 在selenium中可以用到三种等待方式即sleep,implicitly_wait,WebDriverW ...
- Xen入门系列四【Xen 管理实操】
1. 克隆一台虚拟机 # virt-clone -o base -n vm-clone -f /vm/vm-clone.img 参数说明: -o 原虚拟机的名称:必须为关闭或者暂停状态. -n 新虚拟 ...
- [LC] 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- 吴裕雄--天生自然PYTHON学习笔记:解决ElementNotInteractableException: Message: element not interactable
submit=self.wait.until(EC.element_to_be_clickable((By.ID,'loginAction'))) 2.永久覆盖element来保证自己的element ...