爬虫自学之路(二) requests小技巧

中文文档:http://docs.python-requests.org/zh_CN/latest/api.html
1 安装 pip install requests 或者 python -m install requests

2 RequestsCookieJar转换为cookie字典

requests.utils.dict_from_cookiejar(resp.cookies)

3 cookie字典转换为RequestsCookieJar

requests.utils.cookiejar_from_dict({"BDORZ":"27315"})

4 URL编码

requests.utils.quote("编码") # '%E7%BC%96%E7%A0%81'

5 URL解码

requests.utils.unquote("%E7%BC%96%E7%A0%81") # “编码”

6 不验证HTTPS

requests.get("https://www.12306.cn/mormhweb/", verify=False)

7 设置超时时间 和 retrying模块

requests.get("https://www.baidu.cn/", timeout=5)

8 retrying模块

import requests
from retrying import retry

headers={
"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
"Referer": "https://m.douban.com/movie/",
}

@retry(stop_max_attempt_number=3)def _parse_url(url,method,data,proxies):
print("*"*20)
if method=="POST":
response = requests.post(url,data=data,headers=headers,proxies=proxies)
else:
response = requests.get(url,headers=headers,timeout=3,proxies=proxies)
assert response.status_code == 200
return response.content.decode()

def parse_url(url,method="GET",data=None,proxies={}):
try:
html_str = _parse_url(url,method,data,proxies)
except:
html_str = None

return html_str

if __name__ == '__main__':
url = "www.baidu.com"
print(parse_url(url))

9 urldecode 对字符串进行url解码
发起一个get请求时,会在url后携带参数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值