标准库urllib
首先先来看标准库urllib。标准库的优点是Python自带的,不需要安装任何第三方库,缺点就是urllib属于偏底层的库,使用起来比较麻烦。下面是urllib发起请求的一个简单例子,大家看看就好。可以看到为了发起一个简单的请求,我们需要创建opener、request、ProxyHandler等好几个对象,比较麻烦。
importurllib.requestasrequestimportrequestsproxies={'https':'https://127.0.0.1:1080','http':'http://127.0.0.1:1080'}headers={'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64;rv:65.0)Gecko/20100101Firefox/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。
pipinstallrequests
下面是一个简单的例子,和上面urllib示例代码实现的功能相同,但是代码量少多了,也更易读。
print('--------------使用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)
关于requests我就不多做介绍了,因为它有中文文档,虽然比官方落后几个小版本号,不过无伤大雅,大家可以放心参阅。
http://cn.python-requests.org/zh_CN/latest/
beautifulsoup
利用前面介绍的requests类库,我们可以轻易地获取HTML代码,但是为了从HTML中找到所需的数据,我们还需要HTML/XML解析库,BeautifulSoup就是这么一个常用的库。首先先来安装它:
pipinstallbeautifulsoup4
这次就用我简书主页作为例子,爬取一下我简书的文章列表。首先先用requests获取到网页内容。
frompprintimportpprintimportbs4importrequestsheaders={'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64;rv:65.0)Gecko/20100101Firefox/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]forainnote_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/
requests-html
这个类库是requests的兄弟,同样也是Kenneth Reitz大神的作品。它将请求网页和解析网页结合到了一起。本来如果你用requests的话只能请求网页,为了解析网页还得使用BeautifulSoup这样的解析库。现在只需要requests-html一个库就可以办到。首先先来安装。
pipinstallrequests-html
然后我们来看看用requests-html如何重写上面这个例子。
fromrequests_htmlimportHTMLSessionfrompprintimportpprinturl='https://www.jianshu.com/u/7753478e1554'headers={'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64;rv:65.0)Gecko/20100101Firefox/65.0'}session=HTMLSession()r=session.get(url,headers=headers)note_list=r.html.find('ul.note-list',first=True)forainnote_list.find('li>div.content>a.title'):title=a.textlink=f'https://www.jianshu.com{a.attrs["href"]}'print(f'《{title}》,{link}')
requests-html除了可以使用css选择器来搜索以外,还可以使用xpath来查找。
forainr.html.xpath('//ul[@class="note-list"]/li/div[@class="content"]/a[@class="title"]'):title=a.textlink=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)forainr.html.xpath('//ul[@class="note-list"]/li/div[@class="content"]/a[@class="title"]'):title=a.textlink=f'https://www.jianshu.com{a.attrs["href"]}'print(f'《{title}》,{link}')
类似的,今日头条的个人页面也是异步加载的,所以也得调用render函数。
fromrequests_htmlimportHTMLSessionheaders={'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64;rv:65.0)Gecko/20100101Firefox/65.0'}session=HTMLSession()r=session.get('https://www.toutiao.com/c/user/6662330738/#mid=1620400303194116',headers=headers)r.html.render()foriinr.html.find('div.rbox-innera'):title=i.textlink=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
scrapy
以上介绍的几个框架都是各自有各自的作用,把它们结合起来可以达到编写爬虫的目的,但是要说专业的爬虫框架,还是得谈谈scrapy。作为一个著名的爬虫框架,scrapy将爬虫模型框架化和模块化,利用scrapy,我们可以迅速生成功能强大的爬虫。不过scrapy概念众多,要仔细说还得专门开篇文章,这里就只简单演示一下。首先安装scrapy,如果是Windows系统,还需要安装pypiwin32。
pipinstallscrapypipinstallpypiwin32
然后创建scrapy项目并添加一个新爬虫。
scrapystartprojectmyprojectcdmyprojectscrapygenspidermyjianshu.com
打开配置文件settings.py,设置用户代理,否则会遇到403错误。
USER_AGENT='Mozilla/5.0(WindowsNT10.0;Win64;x64;rv:65.0)Gecko/20100101Firefox/65.0'
然后修改一下爬虫。
其实做为一个开发者,有一个学习的氛围跟一个交流圈子是一个特别重要的事情,这里我创建了一个python交流群1005819593,不管你是小白还是大牛欢迎入驻,大家一起交流成长。
#--coding:utf-8--importscrapyclassJianshuSpider(scrapy.Spider):name='jianshu'allowed_domains=['jianshu.com']start_urls=['https://www.jianshu.com/u/7753478e1554']defparse(self,response):forarticleinresponse.css('div.content'):yield{'title':article.css('a.title::text').get(),'link':'https://www.jianshu.com'+article.xpath('a[@class="title"]/@href').get()}
最后运行一下爬虫。
scrapycrawlmy
以上就是这篇文章的内容了,希望对大家有所帮助。