本文共 9705 字,大约阅读时间需要 32 分钟。
点击上方“程序IT圈”,选择“置顶公众号”
正文
现在Python语言大火,在网络爬虫、人工智能、大数据等领域都有很好的应用。今天我向大家介绍一下Python爬虫的一些知识和常用类库的用法,希望能对大家有所帮助。
发起网络请求这个步骤常用的类库有标准库urllib以及Python上常用的requests库。解析网页常用的类库有的BeautifulSoup。另外requests的作者还开发了另一个很好用的库requests-html,提供了发起请求和解析网页的二合一功能,开发小型爬虫非常方便。另外还有一些专业的爬虫类库,其中比较出名的就是scrapy。本文将会简单介绍一下这些类库,之后还会专门写一篇文章介绍scrapy的用法。
标准库urllib
首先先来看标准库urllib。标准库的优点是Python自带的,不需要安装任何第三方库,缺点就是urllib属于偏底层的库,使用起来比较麻烦。下面是urllib发起请求的一个简单例子,大家看看就好。可以看到为了发起一个简单的请求,我们需要创建opener、request、ProxyHandler等好几个对象,比较麻烦。
import urllib.request as requestimport requestsproxies = { 'https': 'https://127.0.0.1:1080', 'http': 'http://127.0.0.1:1080'}headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'}print('--------------使用urllib--------------')url = 'http://www.google.com'opener = request.build_opener(request.ProxyHandler(proxies))request.install_opener(opener)req = request.Request(url, headers=headers)response = request.urlopen(req)print(response.read().decode())as request import requests proxies = { 'https': 'https://127.0.0.1:1080', 'http': 'http://127.0.0.1:1080' } headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0' } print('--------------使用urllib--------------') url = 'http://www.google.com' opener = request.build_opener(request.ProxyHandler(proxies)) request.install_opener(opener) req = request.Request(url, headers=headers) response = request.urlopen(req) print(response.read().decode())
requests
requests是Kenneth Reitz大神的著名作品之一,优点就是极度简单和好用。首先来安装requests。
pip install requests
下面是一个简单的例子,和上面urllib示例代码实现的功能相同,但是代码量少多了,也更易读。
print('--------------使用requests--------------')response = requests.get('https://www.google.com', headers=headers, proxies=proxies)response.encoding = 'utf8'print(response.text)'--------------使用requests--------------') response = requests.get('https://www.google.com', headers=headers, proxies=proxies) response.encoding = 'utf8' print(response.text)
requests还可以方便的发送表单数据,模拟用户登录。返回的Response对象还包含了状态码、header、raw、cookies等很多有用的信息。
data = { 'name': 'yitian', 'age': 22, 'friends': ['zhang3', 'li4']}response = requests.post('http://httpbin.org/post', data=data)pprint(response.__dict__)print(response.text) 'name': 'yitian', 'age': 22, 'friends': ['zhang3', 'li4'] } response = requests.post('http://httpbin.org/post', data=data) pprint(response.__dict__) print(response.text)
关于requests我就不多做介绍了,因为它有中文文档,虽然比官方落后几个小版本号,不过无伤大雅,大家可以放心参阅。
http://cn.python-requests.org/zh_CN/latest//zh_CN/latest/
beautifulsoup
利用前面介绍的requests类库,我们可以轻易地获取HTML代码,但是为了从HTML中找到所需的数据,我们还需要HTML/XML解析库,BeautifulSoup就是这么一个常用的库。首先先来安装它:
pip install beautifulsoup4
这次就用我简书主页作为例子,爬取一下我简书的文章列表。首先先用requests获取到网页内容。
from pprint import pprintimport bs4import requestsheaders = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'}url = 'https://www.jianshu.com/u/7753478e1554'response = requests.get(url, headers=headers)import pprint import bs4 import requests headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0' } url = 'https://www.jianshu.com/u/7753478e1554' response = requests.get(url, headers=headers)
然后就是BeautifulSoup的代码了。在使用BeautifulSoup的时候首先需要创建一个HTML树,然后从树中查找节点。BeautifulSoup主要有两种查找节点的办法,第一种是使用find和find_all方法,第二种方法是使用select方法用css选择器。拿到节点之后,用contents去获取它的子节点,如果子节点是文本,就会拿到文本值,注意这个属性返回的是列表,所以要加[0]。
html = bs4.BeautifulSoup(response.text, features='lxml')note_list = html.find_all('ul', class_='note-list', limit=1)[0]for a in note_list.select('li>div.content>a.title'): title = a.contents[0] link = f'https://www.jianshu.com{a["href"]}' print(f'《{title}》,{link}') note_list = html.find_all('ul', class_='note-list', limit=1)[0] for a in note_list.select('li>div.content>a.title'): title = a.contents[0] link = f'https://www.jianshu.com{a["href"]}' print(f'《{title}》,{link}')
BeautifulSoup也有中文文档,同样也是稍微落后两个小版本,影响不大。
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh//www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
requests-html
这个类库是requests的兄弟,同样也是Kenneth Reitz大神的作品。它将请求网页和解析网页结合到了一起。本来如果你用requests的话只能请求网页,为了解析网页还得使用BeautifulSoup这样的解析库。现在只需要requests-html一个库就可以办到。
pip install requests-html
然后我们来看看用requests-html如何重写上面这个例子。
from requests_html import HTMLSessionfrom pprint import pprinturl = 'https://www.jianshu.com/u/7753478e1554'headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'}session = HTMLSession()r = session.get(url, headers=headers)note_list = r.html.find('ul.note-list', first=True)for a in note_list.find('li>div.content>a.title'): title = a.text link = f'https://www.jianshu.com{a.attrs["href"]}' print(f'《{title}》,{link}')import HTMLSession from pprint import pprint url = 'https://www.jianshu.com/u/7753478e1554' headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0' } session = HTMLSession() r = session.get(url, headers=headers) note_list = r.html.find('ul.note-list', first=True) for a in note_list.find('li>div.content>a.title'): title = a.text link = f'https://www.jianshu.com{a.attrs["href"]}' print(f'《{title}》,{link}')
requests-html除了可以使用css选择器来搜索以外,还可以使用xpath来查找。
for a in r.html.xpath('//ul[@class="note-list"]/li/div[@class="content"]/a[@class="title"]'): title = a.text link = f'https://www.jianshu.com{a.attrs["href"]}' print(f'《{title}》,{link}')in r.html.xpath('//ul[@class="note-list"]/li/div[@class="content"]/a[@class="title"]'): title = a.text link = f'https://www.jianshu.com{a.attrs["href"]}' print(f'《{title}》,{link}')
requests-html还有一个很有用的特性就是浏览器渲染。有些网页是异步加载的,直接用爬虫去爬只能得到一个空页面,因为数据是靠浏览器运行JS脚本异步加载的,这时候就需要浏览器渲染了。而浏览器渲染用requests-html做非常简单,只要多调用一个render函数即可。render函数有两个参数,分别指定页面下滑次数和暂停时间。render函数第一次运行的时候,requests-html会下载一个chromium浏览器,然后用它渲染页面。
session = HTMLSession()r = session.get(url, headers=headers)# render函数指示requests-html用chromium浏览器渲染页面r.html.render(scrolldown=50, sleep=0.2)for a in r.html.xpath('//ul[@class="note-list"]/li/div[@class="content"]/a[@class="title"]'): title = a.text link = f'https://www.jianshu.com{a.attrs["href"]}' print(f'《{title}》,{link}') # render函数指示requests-html用chromium浏览器渲染页面 r.html.render(scrolldown=50, sleep=0.2) for a in r.html.xpath('//ul[@class="note-list"]/li/div[@class="content"]/a[@class="title"]'): title = a.text link = f'https://www.jianshu.com{a.attrs["href"]}' print(f'《{title}》,{link}')
类似的,今日头条的个人页面也是异步加载的,所以也得调用render函数。
from requests_html import HTMLSessionheaders = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'}session = HTMLSession()r = session.get('https://www.toutiao.com/c/user/6662330738/#mid=1620400303194116', headers=headers)r.html.render()for i in r.html.find('div.rbox-inner a'): title = i.text link = f'https://www.toutiao.com{i.attrs["href"]}' print(f'《{title}》 {link}')import HTMLSession headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0' } session = HTMLSession() r = session.get('https://www.toutiao.com/c/user/6662330738/#mid=1620400303194116', headers=headers) r.html.render() for i in r.html.find('div.rbox-inner a'): title = i.text link = f'https://www.toutiao.com{i.attrs["href"]}' print(f'《{title}》 {link}')
最后是requests-html的官网地址以及中文文档。
https://html.python-requests.org/https://cncert.github.io/requests-html-doc-cn/#/?id=requests-html/html.python-requests.org/ https://cncert.github.io/requests-html-doc-cn/#/?id=requests-html
scrapy
以上介绍的几个框架都是各自有各自的作用,把它们结合起来可以达到编写爬虫的目的,但是要说专业的爬虫框架,还是得谈谈scrapy。作为一个著名的爬虫框架,scrapy将爬虫模型框架化和模块化,利用scrapy,我们可以迅速生成功能强大的爬虫。
pip install scrapypip install pypiwin32 pip install pypiwin32
然后创建scrapy项目并添加一个新爬虫。
scrapy startproject myprojectcd myprojectscrapy genspider my jianshu.comcd myproject scrapy genspider my jianshu.com
打开配置文件settings.py,设置用户代理,否则会遇到403错误。
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0''Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
然后修改一下爬虫。
# -*- coding: utf-8 -*-import scrapyclass JianshuSpider(scrapy.Spider): name = 'jianshu' allowed_domains = ['jianshu.com'] start_urls = ['https://www.jianshu.com/u/7753478e1554'] def parse(self, response): for article in response.css('div.content'): yield { 'title': article.css('a.title::text').get(), 'link': 'https://www.jianshu.com' + article.xpath('a[@class="title"]/@href').get() } import scrapy class JianshuSpider(scrapy.Spider): name = 'jianshu' allowed_domains = ['jianshu.com'] start_urls = ['https://www.jianshu.com/u/7753478e1554'] def parse(self, response): for article in response.css('div.content'): yield { 'title': article.css('a.title::text').get(), 'link': 'https://www.jianshu.com' + article.xpath('a[@class="title"]/@href').get() }
最后运行一下爬虫。
scrapy crawl my
以上就是这篇文章的内容了,希望对大家有所帮助。
本文是我的好朋友小乐整理的,公众号程序员小乐,小乐也会分享一些职场经验给同行们,主要分享Java、算法、职场心得、面经等文章,一线大厂干货,以需为本做一个有趣的帮助程序员成长的公众号。每天早上8点20分推送。小乐喜欢乐于助人,强烈建议大家关注他。
~完~
打卡赠书:
送什么书?请点击左下角阅读原文查看!
签到方式:长按以下二维码参与打卡↓↓↓↓↓
今天是第二期打卡的最后一天,恭喜你完成打卡
如果第二期打卡你全部满勤打卡,请加我的助手
备注【满勤】,邀请进群红包抽奖满勤10本书!
或者公众号后台回复:【满勤】二字 。
只有打卡满29天的才能加入,加入需要截图 。
ps:没满勤如果加入,到时也会踢,避免尴尬!
转载地址:https://cxydev.blog.csdn.net/article/details/95937604 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!