基于get请求
import requests
url = "http://www.baidu.com/"
headers = {
'User-Agent':'',
}
response = request.get(url=url, headers=headers)
response.encode = 'utf-8'
print(response.text)
基于post请求
import requests
data = {
}
url = "http://www.baidu.com/"
headers = {
'User-Agent':'',
}
response = request.post(url=url, headers=headers, data=data)
response.encode = 'utf-8'
print(response.text)
代理(proxies参数)
import requests
proxies = {
'http':'127.184.124.29:8118',
}
url = "http://www.baidu.com/"
response = request.get(url=url, proxies=proxies)
print(response.text)
Cookies和Session
Cookies
import requests
response = request.get("http://www.baidu.com/")
cookiejar = response.cookies
cookiedict = request.utils.dict_from_cookiejar(cookiejar)
print(cookiejar)
print(coookiedict)
Session
import session
#创建session对象,可以保存Cookie的值
session = requests.sesssion()
#处理headers
headers = {
'User-Agent':'',
}
#需要登录的用户名和密码
data = {
'email':'name',
'password':'123456',
}
session.post('http://www.renren.com/PLogin.do', data=data)
response = session.get('http://www.renren.com/410043129/profile')
print(response.text)
网友评论