import requests
url = "http://www.baidu.com/s?ie=UTF8&wd=ip"
#代理
proxies = {
'http':'121.41.171.223.3128'
}
#添加头部
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
#使用代理
r = requests.get(url=url,headers=headers,proxies=proxies)
with open('daili.html','wb')as fp:
fp.write(r.content)
#requests模块get参数传输
import requests
url = "http://www.baidu.com"
"""
#添加头部
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
r = requests.get(url,headers)#r是一个响应对象
print(r)
#查看内容 先看编码
# print(r.encoding)
# r.encoding= "utf8"
# print(r.text)
"""
#带参数的get
url = "http://www.baidu.com/s"
#get参数
data = {
'ie':'utf8',
'kw':'中国'
}
#添加头部
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
r = requests.get(url,headers=headers,params=data)
#结果写到文件中
with open('baidu.html','wb') as fp:
fp.write(r.content)
import requests
post_url = "......"
#添加头部
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}
#post数据
form_data = {
'sad':'sada',
'sda':'sdaqs',
'sda':'sad'
}
#post数据发送请求函数
r = requests.post(url=post_url,headers=headers,data=form_data)
print(r.text)