python模拟浏览器请求的库_基于Python模拟浏览器发送http请求

python模拟浏览器请求的库_基于Python模拟浏览器发送http请求

2023年6月27日发(作者:)

python模拟浏览器请求的库_基于Python模拟浏览器发送http请求1.使⽤ urllib2 实现#! /usr/bin/env python# -*- coding=utf-8 -*-import urllib2url=""req_header = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko)Chrome/23.0.1271.64 Safari/537.11","Accept":"text/html;q=0.9,*/*;q=0.8","Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3","Accept-Encoding":"gzip","Connection":"close","Referer":None #注意如果依然不能抓取的话,这⾥可以设置抓取⽹站的host}req_timeout = 5req = t(url,None,req_header)resp = n(req,None,req_timeout)html = ()print(html)2.使⽤ requests 模块(1).get请求#-*- coding:utf-8 -*-import requestsurl = ""payload = {"key1": "value1", "key2": "value2"}r = (url, params=payload)print (2).post请求#-*- coding:utf-8 -*-import requestsurl1 = "/login"#登陆地址url2 = "/main"#需要登陆才能访问的地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;","Accept-Encoding":"gzip","Accept-Language":"zh-CN,zh;q=0.8","Referer":"/","User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90Safari/537.36"}res1 = (url1, data=data, headers=headers)res2 = (url2, cookies=s, headers=headers)print t#获得⼆进制响应内容print #获得原始响应内容,需要stream=Trueprint (50)print type()#返回解码成unicode的内容print nt y#追踪重定向print sprint s["example_cookie_name"]print sprint s["Content-Type"]print ("content-type")print #讲返回内容编码为jsonprint ng#返回内容编码print _code#返回http状态码print _for_status()#返回错误状态码(3).使⽤session对象的写法#-*- coding:utf-8 -*-import requestss = n()url1 = "/login"#登陆地址url2 = "/main"#需要登陆才能访问的地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;","Accept-Encoding":"gzip","Accept-Language":"zh-CN,zh;q=0.8","Referer":"/","User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90Safari/537.36"}prepped1 = t("POST", url1,data=data,headers=headers).prepare()(prepped1)"""也可以这样写res = t("POST", url1,data=data,headers=headers)prepared = e_request(res)# do something with # do something with (prepared)"""prepare2 = t("POST", url2,headers=headers).prepare()res2 = (prepare2)print t"""另⼀种写法"""#-*- coding:utf-8 -*-import requestss = n()url1 = "/login"#登陆地址url2 = "/main"#需要登陆才能访问的页⾯地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;","Accept-Encoding":"gzip","Accept-Language":"zh-CN,zh;q=0.8","Referer":"/","User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90Safari/537.36"}res1 = (url1, data=data)res2 = (url2)print(t)3.其他的⼀些请求⽅式>>> r = ("/put")>>> r = ("/delete")>>> r = ("/get")以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持云海天教程。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687866258a52091.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信