加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

爬虫之requests模块

发布时间:2020-12-17 00:03:12 所属栏目:Python 来源:网络整理
导读:引入 在学习爬虫之前可以先大致的了解一下HTTP协议~ HTTP协议: 爬虫的基本流程 简介 简介:Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,Requests它会比urllib更加方便,可以节约我们大量的工作。一句话,requests

引入

在学习爬虫之前可以先大致的了解一下HTTP协议~

HTTP协议:

爬虫的基本流程

简介

简介:Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库,Requests它会比urllib更加方便,可以节约我们大量的工作。一句话,requests是python实现的最简单易用的HTTP库,建议爬虫使用requests库。默认安装好python之后,是没有安装requests模块的,需要单独通过pip安装

安装方法:pip install requests

开源地址:中文文档 API:?

基本语法

requests模块支持的请求:

) 

get请求

1. 基本请求

=requests.get(with open(<span style="color: #800000;">"<span style="color: #800000;">jd.html<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">wb<span style="color: #800000;">"<span style="color: #000000;">) as f:
f.write(response.content)

2. 含参数请求

=requests.get(=requests.get(,params={:})

3. 含请求头

=requests.get(=:

4. 含cookies请求

url = <span style="color: #800000;">'<span style="color: #800000;">http://httpbin.org/cookies<span style="color: #800000;">'<span style="color: #000000;">
cookies = dict(sbid=<span style="color: #000000;">str(uuid.uuid4()))

res = requests.get(url,cookies=<span style="color: #000000;">cookies)
<span style="color: #0000ff;">print(res.text)

5. request.session()

session=<span style="color: #000000;">requests.session()
res1
=session.get(<span style="color: #800000;">"<span style="color: #800000;">https://www.zhihu.com/explore<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">print<span style="color: #000000;">(session.cookies.get_dict())
res2=session.get(<span style="color: #800000;">"<span style="color: #800000;">https://www.zhihu.com/question/30565354/answer/463324517<span style="color: #800000;">",cookies={<span style="color: #800000;">"<span style="color: #800000;">abs<span style="color: #800000;">":<span style="color: #800000;">"<span style="color: #800000;">123<span style="color: #800000;">"}

post请求

1. data参数requests.post()用法与requests.get()完全一致,特殊的是requests.post()多了一个data参数,用来存放请求体数据

response=requests.post(,params={:},data={:})

2. 发送json数据

=requests.post(url=,data={:}) res2=requests.post(url=<span style="color: #800000;">'<span style="color: #800000;">http://httpbin.org/post<span style="color: #800000;">',json={<span style="color: #800000;">'<span style="color: #800000;">age<span style="color: #800000;">':<span style="color: #800000;">"<span style="color: #800000;">22<span style="color: #800000;">",}) <span style="color: #008000;">#<span style="color: #008000;">默认的请求头:application/json
<span style="color: #0000ff;">print
(res2.json())

response对象

1. 常见属性

=requests.get( (respone.encoding)

2. 编码问题

=requests.get( with open(,

3. 下载二进制文件(图片,视频,音频)

=requests.get(, line

4. 解析json数据

response=requests.get(<span style="color: #800000;">'<span style="color: #800000;">http://httpbin.org/get<span style="color: #800000;">'<span style="color: #000000;">)
res1=json.loads(response.text) <span style="color: #008000;">#<span style="color: #008000;">太麻烦
res2=response.json() <span style="color: #008000;">#<span style="color: #008000;">直接获取json数据
<span style="color: #0000ff;">print(res1==res2)

5. Redirection and History

默认情况下,除了 HEAD,Requests 会自动处理所有重定向。可以使用响应对象的 history 方法来追踪重定向。Response.history 是一个 Response 对象的列表,为了完成请求而创建了这些对象。这个对象列表按照从最老到最近的请求进行排序。

>>> r = requests.get(>>> >>>200 >>>]

另外,还可以通过 allow_redirects 参数禁用重定向处理:

>>> r = requests.get(,allow_redirects=>>>301 >>>

进阶用法

proxies代理

如果需要使用代理,你可以通过为任意请求方法提供 proxies 参数来配置单个请求:

<span style="color: #008000;">#<span style="color: #008000;"> 根据协议类型,选择不同的代理
proxies =<span style="color: #000000;"> {
<span style="color: #800000;">"<span style="color: #800000;">http<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">http://12.34.56.79:9527<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">https<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">http://12.34.56.79:9527<span style="color: #800000;">"<span style="color: #000000;">,}

response = requests.get(<span style="color: #800000;">"<span style="color: #800000;">http://www.baidu.com<span style="color: #800000;">",proxies =<span style="color: #000000;"> proxies)
<span style="color: #0000ff;">print(response.text)

也可以通过本地环境变量 HTTP_PROXY 和 HTTPS_PROXY 来配置代理:

export HTTP_PROXY==

私密代理

<span style="color: #008000;">#<span style="color: #008000;"> 如果代理需要使用HTTP Basic Auth,可以使用下面这种格式:
proxy = { <span style="color: #800000;">"<span style="color: #800000;">http<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">mr_mao_hacker:sffqry9r@61.158.163.130:16816<span style="color: #800000;">"<span style="color: #000000;"> }

response = requests.get(<span style="color: #800000;">"<span style="color: #800000;">http://www.baidu.com<span style="color: #800000;">",proxies =<span style="color: #000000;"> proxy)

<span style="color: #0000ff;">print(response.text)

web客户端验证

如果是Web客户端验证,需要添加 auth = (账户名,密码)

auth=(<span style="color: #800000;">'<span style="color: #800000;">test<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">123456<span style="color: #800000;">'<span style="color: #000000;">)

response = requests.get(<span style="color: #800000;">'<span style="color: #800000;">http://192.168.199.107<span style="color: #800000;">',auth =<span style="color: #000000;"> auth)

<span style="color: #0000ff;">print(response.text)

两个栗子

1、模拟GitHub登录,获取登录信息

<span style="color: #008000;">#<span style="color: #008000;">请求1:
r1=requests.get(<span style="color: #800000;">'
<span style="color: #800000;">https://github.com/login<span style="color: #800000;">'<span style="color: #000000;">)
r1_cookie=r1.cookies.get_dict() <span style="color: #008000;">#<span style="color: #008000;">拿到初始cookie(未被授权)
authenticity_token=re.findall(r<span style="color: #800000;">'<span style="color: #800000;">name="authenticity_token".?value="(.?)"<span style="color: #800000;">',r1.text)[0] <span style="color: #008000;">#<span style="color: #008000;">从页面中拿到CSRF TOKEN
<span style="color: #0000ff;">print(<span style="color: #800000;">"<span style="color: #800000;">authenticity_token<span style="color: #800000;">"<span style="color: #000000;">,authenticity_token)
<span style="color: #008000;">#<span style="color: #008000;">第二次请求:带着初始cookie和TOKEN发送POST请求给登录页面,带上账号密码
data=<span style="color: #000000;">{
<span style="color: #800000;">'<span style="color: #800000;">commit<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">Sign in<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">utf8<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">?<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">authenticity_token<span style="color: #800000;">'<span style="color: #000000;">:authenticity_token,<span style="color: #800000;">'<span style="color: #800000;">login<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">你的github账号?<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">password<span style="color: #800000;">':<span style="color: #800000;">'<span style="color: #800000;">你的密码<span style="color: #800000;">'<span style="color: #000000;">
}

<span style="color: #008000;">#<span style="color: #008000;">请求2:
r2=requests.post(<span style="color: #800000;">'<span style="color: #800000;">https://github.com/session<span style="color: #800000;">'<span style="color: #000000;">,data=<span style="color: #000000;">data,cookies=<span style="color: #000000;">r1_cookie,<span style="color: #008000;">#<span style="color: #008000;"> allow_redirects=False
<span style="color: #000000;"> )
<span style="color: #0000ff;">print(r2.status_code) <span style="color: #008000;">#<span style="color: #008000;">200
<span style="color: #0000ff;">print(r2.url) <span style="color: #008000;">#<span style="color: #008000;">看到的是跳转后的页面:https://github.com/
<span style="color: #0000ff;">print(r2.history) <span style="color: #008000;">#<span style="color: #008000;">看到的是跳转前的response:[<Response [302]>]
<span style="color: #0000ff;">print(r2.history[0].text) <span style="color: #008000;">#<span style="color: #008000;">看到的是跳转前的response.text
<span style="color: #000000;">
with open(<span style="color: #800000;">"<span style="color: #800000;">result.html<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">wb<span style="color: #800000;">"<span style="color: #000000;">) as f:

f.write(r2.content)</span></pre>

2、爬取豆瓣电影信息

<span style="color: #0000ff;">import<span style="color: #000000;"> re
<span style="color: #0000ff;">import<span style="color: #000000;"> json
<span style="color: #0000ff;">import<span style="color: #000000;"> time
<span style="color: #0000ff;">from concurrent.futures <span style="color: #0000ff;">import<span style="color: #000000;"> ThreadPoolExecutor
pool=ThreadPoolExecutor(50<span style="color: #000000;">)

<span style="color: #0000ff;">def<span style="color: #000000;"> getPage(url):

response</span>=<span style="color: #000000;"&gt;requests.get(url)
</span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; response.text

<span style="color: #0000ff;">def<span style="color: #000000;"> parsePage(res):

com</span>=re.compile(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;<div class="item"&gt;.*?<div class="pic"&gt;.*?<em .*?>(?P<id>d+).*?<span class="title"&gt;(?P<title>.*?)</span></span><span style="color: #800000;"&gt;'</span>
               <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;.*?<span class="rating_num" .*?>(?P<rating_num>.*?)</span>.*?<span>(?P<comment_num>.*?)评价</span></span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;,re.S)

iter_result</span>=<span style="color: #000000;"&gt;com.finditer(res)

</span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; iter_result

<span style="color: #0000ff;">def<span style="color: #000000;"> gen_movie_info(iter_result):
<span style="color: #0000ff;">for i <span style="color: #0000ff;">in<span style="color: #000000;"> iter_result:
<span style="color: #0000ff;">yield<span style="color: #000000;"> {
<span style="color: #800000;">"<span style="color: #800000;">id<span style="color: #800000;">":i.group(<span style="color: #800000;">"<span style="color: #800000;">id<span style="color: #800000;">"<span style="color: #000000;">),<span style="color: #800000;">"<span style="color: #800000;">title<span style="color: #800000;">":i.group(<span style="color: #800000;">"<span style="color: #800000;">title<span style="color: #800000;">"<span style="color: #000000;">),<span style="color: #800000;">"<span style="color: #800000;">rating_num<span style="color: #800000;">":i.group(<span style="color: #800000;">"<span style="color: #800000;">rating_num<span style="color: #800000;">"<span style="color: #000000;">),<span style="color: #800000;">"<span style="color: #800000;">comment_num<span style="color: #800000;">":i.group(<span style="color: #800000;">"<span style="color: #800000;">comment_num<span style="color: #800000;">"<span style="color: #000000;">),}

<span style="color: #0000ff;">def<span style="color: #000000;"> stored(gen):
with open(<span style="color: #800000;">"<span style="color: #800000;">move_info.txt<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">a<span style="color: #800000;">",encoding=<span style="color: #800000;">"<span style="color: #800000;">utf8<span style="color: #800000;">"<span style="color: #000000;">) as f:
<span style="color: #0000ff;">for line <span style="color: #0000ff;">in<span style="color: #000000;"> gen:
data=json.dumps(line,ensure_ascii=<span style="color: #000000;">False)
f.write(data+<span style="color: #800000;">"<span style="color: #800000;">n<span style="color: #800000;">"<span style="color: #000000;">)

<span style="color: #0000ff;">def<span style="color: #000000;"> spider_movie_info(url):
res=<span style="color: #000000;">getPage(url)
iter_result=<span style="color: #000000;">parsePage(res)
gen=<span style="color: #000000;">gen_movie_info(iter_result)
stored(gen)

<span style="color: #0000ff;">def<span style="color: #000000;"> main(num):

url</span>=<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;https://movie.douban.com/top250?start=%s&amp;filter=</span><span style="color: #800000;"&gt;'</span>%<span style="color: #000000;"&gt;num
pool.submit(spider_movie_info,url)
</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;spider_movie_info(url)</span>

<span style="color: #0000ff;">if <span style="color: #800080;">name == <span style="color: #800000;">'<span style="color: #800000;">main<span style="color: #800000;">'<span style="color: #000000;">:
before=<span style="color: #000000;">time.time()
count=<span style="color: #000000;">0
<span style="color: #0000ff;">for i <span style="color: #0000ff;">in range(10<span style="color: #000000;">):
main(count)
count+=25<span style="color: #000000;">
after=<span style="color: #000000;">time.time()

</span><span style="color: #0000ff;"&gt;print</span>(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;总共耗费时间:</span><span style="color: #800000;"&gt;"</span>,after-before)</pre>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读