爬虫Requests基本使用

Requests基本使用

安装

  • pip install requests

一、Requests模块请求

  • 获取网页(不带参数)
r = requests.get('http://www.chinahufei.com')
r = requests.post('http://www.chinahufei.com')
r = requests.delete('http://www.chinahufei.com')
r = requests.head('http://www.chinahufei.com')
r = requests.options('http://www.chinahufei.com')
  • 获取网页(带参数)
# get方式
r = requests.get("http://api.chinahufei.com", params = { 'page': 1 })
# post方式
r = requests.post('http://api.chinahufei.com', data = {'kwd':'hufei'})
# 通用方式
r = requests.request("get", "http://api.chinahufei.com/")
# 其他
payload = {'page': '1', 'kwd': ['hufei', 'china']}
r = requests.get('http://api.chinahufei.com', params=payload)
  • 获取网页(带header和UserAgent)
# get方式
kw = {'kwd':'长城'}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
# params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("http://api.chinahufei.com", params = kwd, headers = headers)

# post方式
formdata = {
    "type":"AUTO",
    "i":"i love python",
    "doctype":"json",
    "xmlVersion":"1.8",
    "keyfrom":"fanyi.web",
    "ue":"UTF-8",
    "action":"FY_BY_ENTER",
    "typoResult":"true"
}
url = "http://api.chinahufei.com"
headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
r = requests.post(url, data = formdata, headers = headers)
  • 获取网页(使用代理)
import requests
# 根据协议类型,选择不同的代理
proxies = {
  "http": "http://12.34.56.79:9527",
  "https": "http://12.34.56.79:9527"
}

response = requests.get("http://api.chinahufei.com", proxies = proxies)
print response.text

# 私密代理验证
import requests
	# 如果代理需要使用HTTP Basic Auth,可以使用下面这种格式:
proxy = { "http": "mr_mao_hacker:[email protected]:16816" }
response = requests.get("http://api.chinahufei.com", proxies = proxy)
print response.text
# Web客户端验证
import requests
auth=('test', '123456')
response = requests.get('http://192.168.199.107', auth = auth)
print response.text
  • 获取网页(重定向使用)
# 不允许
r = requests.head('http://github.com', allow_redirects=False)
  • HTTPS请求 SSL证书验证
# 如果我们想跳过 12306 的证书验证,把 verify 设置为 False 就可以正常请求了。
r = requests.get("https://www.12306.cn/mormhweb/", verify = False)

二、Request模块响应

  • 响应内容-text(Unicode格式的数据)
  • 响应内容-content(字节流数据)
  • 响应内容-json(json类型的数据)
  • url地址-url(完整地址)
  • 响应码-status_code
  • 响应头-headers()
  • 响应头部字符编码-encoding
  • Cookies-cookies
import requests
response = requests.get("http://www.baidu.com/")
# 返回CookieJar对象
cookiejar = response.cookies
# 将CookieJar转为字典
cookiedict = requests.utils.dict_from_cookiejar(cookiejar)
print cookiejar
print cookiedict
  • Sission-session
# 人人网模拟登录
import requests
# 1. 创建session对象,可以保存Cookie值
ssion = requests.session()
# 2. 处理 headers
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
# 3. 需要登录的用户名和密码
data = {"email":"[email protected]", "password":"alarmchime"}  
# 4. 发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里
ssion.post("http://www.renren.com/PLogin.do", data = data)
# 5. ssion包含用户登录后的Cookie值,可以直接访问那些登录后才可以访问的页面
response = ssion.get("http://www.renren.com/410043129/profile")
# 6. 打印响应内容
print response.text
  • 响应历史-history

三、Request模块的编解码问题3种解决方法

  • response.content.decode()
  • response.content.decode('gbk')
  • response.text
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章