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 requestsurl = "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)"""#带参数的geturl = "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)
原文地址:https://www.cnblogs.com/Qiuzhiyu/p/12183170.html
时间: 2024-10-24 07:04:52