pip install requests
。get(url,params,headers)其中,参数 url 表示请求 URL;参数 params 表示 GET 方法的请求参数;参数 headers 表示请求头的信息。
属 性 | 描 述 | 方 法 | 描 述 |
---|---|---|---|
text | 字符串类型的响应内容 | json() | 将响应内容解析为 JSON 格式 |
content | 字节类型的响应内容 | ||
url | 响应的 URL | ||
status_code | 响应状态码 | ||
headers | 响应头中的全部信息 | ||
encoding | 设置编码格式 |
import requests url="http://www.baidu.com" response=requests.get(url=url) response.encoding="utf-8" print(response.url) print(response.status_code) print(response.headers) res=response.text with open("baidu.html","w",encoding="utf-8")as f: f.write(res)
import requests url="http://www.oldxia.com/xzd/upload/static/image/common/logo.png" response=requests.get(url=url) #返回二进制的数据 res=response.content with open("logo.png","wb")as f: f.write(res)
import requests url="http://www.so.com" headers={ "User-Agent":"Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML, like Gecko)Chrome/74.0.3729.169 Safari/537.36", } response=requests.get(url=url,headers=headers) response.encoding="utf-8" res=response.text with open("360.html","w",encoding="utf-8")as f: f.write(res)
post(url,data,headers)其中,参数 url 表示请求 URL;参数 data 表示 POST 方法的请求参数;参数 headers 表示请求头的信息。
import requests url="http://www.oldxia.com/http_test/post.php" data={'name':'夏正东','age':35} response=requests.post(url=url,data=data) res=response.text print(res)
import requests url="https://fanyi.baidu.com/sug" name=input("请输入要查询的单词:") headers={ "User-Agent":"Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/74.0.3729.169 Safari/537.36", } data={ "kw":name } response=requests.post(url=url,headers=headers,data=data) #响应头中的Content-Type为application/json res=response.json() print(res["data"][0]["v"])
import requests url="https://www.kuaidaili.com/usercenter/" headers={ "User-Agent":"Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML, like Gecko)Chrome/74.0.3729.169 Safari/537.36", "Cookie":"channelid=0;sid=1629544743008400;_gcl_au=1.1.1908873876.1629544864;_ga=GA1.2.810344698.1629544865;_gid=GA1.2.2144128861.1629544865;Hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31=1629544865,1629599040; Hm_lpvt_7ed65b1cc4b810e9fd37959c9bb51b31=1629599043;sessionid=5465c0121d88e755b9ecad8b82109136" } response=requests.get(url=url,headers=headers) response.encoding="utf-8" res=response.text with open("kuaidaili.html","w",encoding="utf-8")as f: f.write(res)
import requests #创建session对象,该对象会自动保存Cookie,并且可以实现会话保持 session=requests.Session() url="https://www.kuaidaili.com/login/" #POST方法的请求参数 data={ "username":"13309861086", "passwd":"www.oldxia.com" } headers={ "User-Agent":"Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML, like Gecko)Chrome/74.0.3729.169 Safari/537.36", } #向登录页面发起HTTP请求,目的是获取成功登录后的Cookie,然后保存到session对象中 response=session.post(url=url,data=data,headers=headers) new_url="https://www.kuaidaili.com/usercenter/" #使用已经自动保存Cookie的session对象向账户管理页面发起HTTP请求 new_response=session.get(url=new_url,headers=headers) res=new_response.text with open("kuaidaili.html","w",encoding="utf-8")as f: f.write(res)
import requests url='https://www.so.com/s?q=ip' #免费代理IP #proxy={ #"http":"http://42.56.238.40:3000", #"https":"http://42.56.238.40:3000" #} #收费代理IP proxy={ "http":"http://xiazhengdong:3p0h090r@110.85.202.232:18937", "https":"http://xiazhengdong:3p0h090r@110.85.202.232:18937" } headers={ "User-Agent":"Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML, like Gecko)Chrome/74.0.3729.169 Safari/537.36", } response=requests.get(url=url,headers=headers,proxies=proxy) res=response.text with open("so.html","w",encoding="utf-8")as f: f.write(res)此时,运行 so.html 文件,其显示的 IP 为“223.11.213.168中国山西省太原市中国电信”,而笔者的真实 IP 为“113.234.106.225中国辽宁省大连市中国联通”。
本文链接:http://task.lmcjl.com/news/18075.html