虫师 博客:https://www.cnblogs.com/fnng/
结论:HTTPS 比 HTTP 更安全,但是性能要低一点。
注意:加粗的请求头为常用请求头,在服务器被用来进行爬虫识别的频率最高,相较于其余的请求头更为重要。
注意:所有的状态码都不可信,一切以是否从抓包得到的响应中获取到数据为准。
根据代理 ip 的匿名程序,代理 IP 可以分为下面三类:
透明代理:透明代理虽然可以直接“隐藏”你的 IP 地址,但还是可以查到你是谁。目标服务器接收到的请求头如下:REMOTE_ADDR = Proxy IPHTTP_VIA = Proxy IPHTTP_X_FORWARDED_FOR = Your IP匿名代理:使用匿名代理,别人只能知道你用了代理,无法知道你是谁。目标服务器接收到的请求头如下:REMOTE_ADDR = Proxy IPHTTP_VIA = Proxy IPHTTP_X_FORWARDED_FOR = Proxy IP高匿代理:高匿代理让别人根本无法发现你在使用代理,所以高匿代理是最好的选择。毫无疑问使用高匿代理效果最好。目标服务器接收到的请求头如下:REMOTE_ADDR = Proxy IPHTTP_VIA = not determinedHTTP_X_FORWARDED_FOR = not determined根据网站所使用的协议不同,需要使用相应协议的代理服务。从代理服务请求使用的协议可以分为:
http 代理:目标 url 为 http 协议https 代理:目标 url 为 https 协议socks 隧道代理(例如 socks5 代理)等:socks 代理只是简单地传递数据包,不关心是何种应用协议(FTP、HTTP 和 HTTPS 等)socks 代理比 http、https 代理耗时少socks 代理可以转发 http 和 https 的请求pip/pip3 install requestsimport requestsurl = 'http://www.baidu.com'# 向目标url发送get请求response = requests.get(url)# print(response.encoding) # ISO-8859-1# 打印响应内容(中文可能会乱码)print(response.text)注意:response.text 是 requests 模块按照 chardet 模块推测出的编码字符集进行解码的结果。
response.text 和 response.content 的区别:
response.text类型:str解码类型:requests 模块自动根据 HTTP 头部,对响应的编码作出有根据的推测,推测出文本的编码。response.content类型:bytes解码类型:没有指定# ####### 指定 response.text 编码 ######## 手动指定编码格式response.encoding = 'utf8'print(response.text)# ####### 指定 response.content 编码 ######## response.content 是存储的bytes类型的响应源码,可以进行decode操作# print(response.content.decode('gbk'))print(response.content.decode()) # 默认utf-8import requestsurl = 'http://www.baidu.com'# 向目标url发送get请求response = requests.get(url)# 打印响应内容# print(response.text)# print(response.content.decode())print(response.url) # 响应的urlprint(response.status_code) # 响应的状态码print(response.request.headers) # 响应对象的请求头print(response.headers) # 响应头print(response.request._cookies) # 请求携带的cookiesprint(response.cookies) # 响应中携带的cookiesrequests.get(url, headers=headers):
headers 参数接收字典形式的请求头请求头字段名为 key,字段对应的值为 valueimport requestsurl = 'http://www.baidu.com'# 构建请求头字典headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}# 发送带请求头的请求response = requests.get(url, headers=headers)print(response.content.decode())import requestsheaders = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}# 方式一:在url中携带参数# url = 'http://www.baidu.com/s?wd=python'# response = requests.get(url, headers=headers)# 方式二:通过params携带参数url = 'http://www.baidu.com/s'# 构建参数字典data = { 'wd': 'python'}response = requests.get(url, headers=headers, params=data)url = 'https://github.com/GitLqr'# 方式一:在headers参数中携带cookie# 构建请求头# headers = {# ...,# 'Cookie': '_octo=GH1.1.1045146750.1615451260; balabala...',# }# response = requests.get(url, headers=headers)# 方式二:使用 cookies 参数(字典)# 注意:需要字符串 --> 字典temp = '_octo=GH1.1.1045146750.1615451260; _device_id=cd8d64981fcb3fd4ba7f587873e97804;balabala...'cookie_list = temp.split('; ')# 稳妥方案# cookies = {}# for cookie in cookie_list:# cookies[cookie.split('=')[0]] = cookie.split('=')[-1]# 推导式方案cookies = {cookie.split('=')[0]: cookie.split('=')[-1] for cookie in cookie_list}response = requests.get(url, cookies=cookies)# cookieJar对象 转换为 cookies字典dict_cookies = requests.utils.dict_from_cookiejar(response.cookies)# cookies字典 转换为 cookieJar对象jar_cookies = requests.utils.cookiejar_from_dict(dict_cookies)requests.get(url, timeout=3):
timeout=3 表示:发送请求后,3 秒内返回响应,否则就抛出异常import requestsurl = 'https://twitter.com'response = requests.get(url, timeout=3)response = requests.get(url, proxies=proxies):
proxies 类型:字典import requestsurl = 'http://www.baidu.com'proxies = { 'http': 'http://61.160.210.234:808', 'https': 'https://61.160.210.234:808',}response = requests.get(url, proxies=proxies)注意:代理使用成功不会有任何报错,能成功获取响应;如果失败,要么卡滞,要么报错。
免费代理 ip 提供站:http://www.66ip.cn/https://www.kuaidaili.com/free/http://www.data5u.com/http://proxy.mimvp.com/freeopen当某些网站的 CA 证书没有经过【受信任的根证书颁发机构】的认证,访问时会被拦截,浏览器会显示【您的连接不是私密连接】等提示,这时有 2 种解决方案:
方式一:下载该网站的 CA 证书,手动导入给当前设备方式二:使用 response = requests.get(url, verify=False) 直接忽略 CA 证书验证verify 参数能够忽略 CA 证书的认证import requestsurl = 'https://sam.huat.edu.cn:8443/selfservice/'# 报错:requests.exceptions.SSLError: HTTPSConnectionPool(host='sam.huat.edu.cn', port=8443): Max retries exceeded with url: /selfservice/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1125)')))# response = requests.get(url)# 警告:InsecureRequestWarning: Unverified HTTPS request is being made to host '127.0.0.1'. Adding certificate verification is strongly advised.response = requests.get(url, verify=False)response = requests.post(url, data):
data 参数接收一个字典request 模块发送 post 请求函数的其它参数和发送 get 请求的参数完全一致import requestsimport jsonimport sysclass King(object): def __init__(self, word): self.url = 'https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_ciba&sign=37218aa29f55fdcc' self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36', } self.data = { 'from': 'auto', 'to': 'auto', 'q': word } def get_data(self): # 使用post方法发送一个post请求,data为请求体的字典 response = requests.post(self.url, data=self.data, headers=self.headers) return response.content def parse_data(self, data): # loads方法将json字符串转换成python字典 dict_data = json.loads(data) print(dict_data['content']['out']) def run(self): response = self.get_data() print(response) self.parse_data(response)if __name__ == '__main__': # word = input('请输入要翻译的单词或句子:') # word = sys.argv[1] word = '字典' king = King(word=word) king.run()requests 模块中的 Session 类能够自动处理发送请求获取响应过程中产生的 cookie,进而达到状态保持的目的。
作用:自动处理 cookie场景:连续的多次请求使用方法:session = requests.session() # 实例化session对象response = session.get(url, headers, ...)response = session.post(url, data, ...)注意:session 对象发送 get 或 post 请求的参数,与 requests 模块发送请求的参数完全一致
版权声明:我们致力于保护作者版权,注重分享,被刊用文章【66代理ip(Python)】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!;
工作时间:8:00-18:00
客服电话
电子邮件
beimuxi@protonmail.com
扫码二维码
获取最新动态
