Python爬虫 requests库 get/post请求 获取响应内容

您所在的位置:网站首页 python获取xhr Python爬虫 requests库 get/post请求 获取响应内容

Python爬虫 requests库 get/post请求 获取响应内容

2023-12-16 00:59| 来源: 网络整理| 查看: 265

import requests r=requests.get('http://www.lingzhilab.com/resources/getAllRes?page=1') # u'文本内容' bytes 类型 print(r.content) # 获取内容并设置编码 print(r.content.decode('utf-8')) # 文本编码 print(r.apparent_encoding) # utf-8 # 响应状态码 print(r.status_code) # 200 # 文本内容 print(r.text) # JSON解码器 print(r.json())

传递URL参数:

GET请求

import requests key_dist={'page':1,'page':2} # 原本url地址:http://www.lingzhilab.com/resources/getAllRes?page=1 r=requests.get('http://www.lingzhilab.com/resources/getAllRes',params=key_dist) # url正确编码 print(r.url) # http://www.lingzhilab.com/resources/getAllRes?page=2 # u'文本内容' print(r.text)

POST请求

import requests import json key_dist={'page':1,'page':2} # 原本url地址:http://www.lingzhilab.com/resources/getAllRes?page=1 #r=requests.post('http://www.lingzhilab.com/resources/getAllRes',data=key_dist) r=requests.post('http://www.lingzhilab.com/resources/getAllRes',data=json.dumps(key_dist)) # json参数 # url正确编码 print(r.url) # http://www.lingzhilab.com/resources/getAllRes # u'文本内容' print(r.text)

设置超时时间

秒数设置为0.001秒,会抛出异常,一般会把这个值设置为20秒

import requests link="http://www.lingzhilab.com/resources/getAllRes?page=1" # 原本url地址:http://www.lingzhilab.com/resources/getAllRes?page=1 r=requests.get(link,timeout=0.001)

定制请求头

import requests headers={ 'Host': 'www.lingzhilab.com', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36' } r=requests.get('http://www.lingzhilab.com/',headers=headers) print(r.status_code) # 200

构造session

headers={ 'session':'JSESSIONID', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36' }

reqeusts底层是urllib库

参考:Python3 Requests 模块 - 梦徒 - 博客园



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3