Python爬虫爬取Html中的Url常用知识
来源:互联网 发布:安知玉如意百度云 编辑:程序博客网 时间:2024/06/05 06:14
近一段时间在学习如何使用Python进行网络爬虫,越来越觉得Python在处理爬虫问题是非常便捷的,那么接下来我就陆陆续续的将自己学习的爬虫知识分享给大家。
首先在进行实战前,我们需要了解几个常用的函数和正则表达式:
一、几个常用的函数
这里介绍的函数是来自于requests扩展包,他们是findall,search和sub函数:
findall(pattern, string, flags=0)
pattern:为正则表达式
string:为字符串
search(pattern, string, flags=0)
pattern:为正则表达式
string:为字符串
findall与search的区别
findall将遍历所有满足条件的内容,而search一般与group(n)搭配使用,选出满足条件的某个内容。
sub(pattern, repl, string, count=0, flags=0)
pattern:为正则表达式
repl:需要替换的内容
string:为原字符串
二、几个常用的爬虫正则表达式
Python爬虫最常用的三个正则表达式为点号,星号,问号和圆括号:
点号:匹配除“\r\n”之外的任何单个字符,可以理解为一个占位符
举例:
x = '1q2wwyxliuwyx3e4rwyxshunwyx1q2wwyxxiangwyx3e4r'
re.findall('wyx.',x)
Out[1]: ['wyxl', 'wyx3', 'wyxs', 'wyx1', 'wyxx', 'wyx3']
返回"wyx"和紧跟其后的第一个字符
re.findall('wyx...',x)
Out[2]: ['wyxliu', 'wyx3e4', 'wyxshu', 'wyx1q2', 'wyxxia', 'wyx3e4']
返回"wyx"和紧跟其后的第一第二个字符
所以可以将点号(.)理解为一个占位符,而且这个占位符可以代表一切字符。
星号:匹配前一个字符任意次
x = '1q2wwyxliuwyx3e4rwyxshunwyx1q2wwyxxiangwyx3e4r'
re.findall('wyx*',x)
Out[3]: ['wyx', 'wyx', 'wyx', 'wyx', 'wyxx', 'wyx']
星号(*)前一个字符为x,所以返回结果中可以找到x的任意次,x字符串中有两个连续的x,所以返回结果中第5个元素就会有两个x。
问号:匹配前一个字符0次或1次,与星号的不同在于其最多匹配一次。
x = '1q2wwyxliuwyx3e4rwyxshunwyx1q2wwyxxiangwyx3e4r'
re.findall('wyx?',x)
Out[4]: ['wyx', 'wyx', 'wyx', 'wyx', 'wyx', 'wyx']
结果显示,返回结果的第五个元素仅含有一个x。
点星组合(.*):贪婪算法,尽可能多的匹配数据
re.findall('wyx.*wyx',x)
Out[5]: ['wyxliuwyx3e4rwyxshunwyx1q2wwyxxiangwyx']
从返回的结果就可以理解“贪婪”的概念了,结果一次性返回wyx与wyx之间的所有内容。
点星问组合(.*?):非贪婪算法,尽可能少的匹配数据
re.findall('wyx.*?wyx',x)
Out[6]: ['wyxliuwyx', 'wyxshunwyx', 'wyxxiangwyx']
而这次的返回结果就完全不同于上面的结果,它尽可能少的返回满足正则表达式的结果,从而将1大串切割为3小串。
圆括号():返回所需信息
re.findall('wyx(.*?)wyx',x)
Out[7]: ['liu', 'shun', 'xiang']
很明显,通过括号的操作,就直接将想提取的内容抠下来了。
三、半自动化的图片爬虫
本次爬虫的实验来自于当当网有关Python书籍的url,即:
url = http://search.dangdang.com/?key=Python&act=input
在爬取该网页的图片之前,我们需要了解一下网页源代码中有关图片的模式:
发现关于图片的链接存在两种模式,即"<img src='(.*?)' alt"和"img data-original='(.*?)' src",所以我们需要按两种方式提取图片。
首先,将网页源代码复制出来,粘贴到Pic文本文件中,并将内容读取到Content对象中:
f = open('Pic.txt','r')
Content = f.read()
f.close
其次,分别用两种模型提取出图片链接
pic_url = re.findall("<img src='(.*?)' alt",Content)
print pic_url
pic_url2 = re.findall("img data-original='(.*?)' src",Content)
print pic_url2
以上两种模式的图片链接已下载好,接下来就是将这两个列表合并:
pic_url.extend(pic_url2)
最后,通过遍历pic_url中的图片链接,将图片下载并保存到指定的目录下:
import requests #导入所需扩展包
i = 0
for url in pic_url: #开始遍历pic_url中的每个元素
print 'Downloding: ' + url
Pic = requests.get(url)
fp = open('Pic\\' + str(i) + '.jpg','wb') #保存文件
fp.write(Pic.content) #将文件写入到指定的目录文件夹下
fp.close()
i = i + 1
我们看看Pic文件夹是否含有下载好了的文件呢?
Perfect,网页中的图片全都下载下来啦,很简单吧。有兴趣的同学还不赶快动手试试?
- Python爬虫爬取Html中的Url常用知识
- python爬虫实现爬取静态html
- python爬虫-豆瓣爬取数据保存为html文件
- Python爬虫——爬取网页中的图片小试牛刀
- 爬虫爬取音乐url大全
- python爬虫爬取段子
- python爬虫爬取图片
- python爬虫爬取网络小说
- Python 爬虫 爬取视频
- python爬虫爬取csdn
- Python 网络爬虫 002 (入门) 爬取一个网站之前,要了解的知识
- python 爬取知网url
- python的【爬虫】:使用urllib爬取wiki文章,使用beautifulSoup解析html
- python 3.3 爬虫之爬取图片
- python爬虫爬取美女图片
- Python爬虫 爬取豆瓣读书
- 《python爬虫实战》:爬取图片
- python爬虫爬取糗事百科
- Codeforces 742A Arpa’s hard exam and Mehrdad’s naive cheat 打表+水题
- Redis数据类型-String
- 大数 斐波那契 与阶乘
- tableView内的View悬停在导航栏的效果
- 常见的Python面试题+详细解答
- Python爬虫爬取Html中的Url常用知识
- spring mvc <mvc:annotation-driven/> 和<context:component-scan base-package=""/>解释
- Redis数据类型-List
- 修复miniblink无法显示某个网站的某个图标的bug
- Codeforces 742B B. Arpa’s obvious problem and Mehrdad’s terrible solution
- android入门小知识
- Tablayout使用小结
- 第十二章-无线传感网接入Internet
- 常见对象_BigDecimal的加减乘除法的使用