爬虫最常用的模块:requests
Python标准库中提供了:urllib、urllib2、httplib等模块以供Http请求,但是他不好用。一般情况下我们都会使用一个第三方模块requests来发送http请求。requests发送请求的方式一般有2 种,get请求和post请求.
requests的安装
#cmd下执行
pip install requests
或者通过下载requests包之后再安装,一般推荐使用第一种方法。
1.get请求
1 import requests #引用requests模块 2 #通过发送get请求获取百度主页页面 3 ret = requests.get("www.baidu.com") 4 ret.encoding = "utf-8" #编码方式 5 print(ret.text) #打印返回的页面文本,是html格式文本
#res.content返回页面以字节形式显示的内容
requests的get请求可以带参数:url 、[params]、[headers]、[cookies]
2.post请求
import requests url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} headers = {'content-type': 'application/json'} ret = requests.post(url, data=json.dumps(payload), headers=headers) print(ret.text) print(ret.cookies)
更多详细参数可以进入python官网查看
自动登录抽屉
import requests url="https://dig.chouti.com/all/hot/recent/1" header={ "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" } response = requests.get( url=pageurl, headers = header ) cookie1_dict = response.cookies.get_dict() # 发送post请求,进行登录 data = { "phone":"8613121758648", "password":"woshiniba", "oneMonth":1 } response1 = requests.post(url="https://dig.chouti.com/login", data=data, headers=header, cookies = cookie1_dict ) print(response1.text)#打印登录成功后的页面