request库
Posted doupi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了request库相关的知识,希望对你有一定的参考价值。
------------恢复内容开始------------
GET方法
#发送get请求
import requests
headers={‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/79.0.3945.88 Safari/537.36‘}
result = requests.get(url=‘https://movie.douban.com‘,headers=headers)
响应状态码
result.status_code
响应内容编码方式
result.encoding
响应文本
result.text
响应二进制内容
result.content
请求url
result.url
get方法包含请求参数
import requests data={ ‘search_text‘:‘测试‘, ‘cat‘:1002 } headers={‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36‘} result = requests.get(url=‘https://search.douban.com/movie/subject_search‘,headers=headers,params=data)
POST方法
post方法是通过表单的方式提交数据,而get是在url中明文显示数据
使用requests构造POST请求
import requests data={‘first‘:‘false‘,‘pn‘:3,‘kd‘:‘自动化测试工程师‘} headers = { ‘content-type‘:‘application/x-www-form-urlencoded; charset=UTF-8‘, ‘cookie‘:‘_ga=GA1.2.967307923.1577529264; user_trace_token=20191228183418-9a0cf818-295d-11ea-a707-5254005c3644; LGUID=20191228183418-9a0cfd46-295d-11ea-a707-5254005c3644; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2210302027%22%2C%22%24device_id%22%3A%2216f4c1271413e2-0d7b499d0fec92-7711439-1024000-16f4c127142ab7%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24os%22%3A%22Windows%22%2C%22%24browser%22%3A%22Chrome%22%2C%22%24browser_version%22%3A%2280.0.3987.149%22%7D%2C%22first_id%22%3A%2216f4c1271413e2-0d7b499d0fec92-7711439-1024000-16f4c127142ab7%22%7D; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1590928768,1591105016; _gat=1; LGSID=20200602213740-ee435c58-af53-400c-9a60-983e2381d4da; PRE_UTM=m_cf_cpt_baidu_pcbt; PRE_HOST=bzclk.baidu.com; PRE_SITE=http%3A%2F%2Fbzclk.baidu.com%2Fadrc.php%3Ft%3D06KL00c00fA1VH%5F0w3dM0FNkUs09NVuX00000rUBx7C00000TNbZC%5F.THL0oUhY1x60UWdBmy-bIfK15yR1nvmvnWfYnj0snHmsujD0IHdarHfYf1fYfYn4PWuKPWTYPDDLnjTzrRuAf1b4PWnYPfK95gTqFhdWpyfqn1nvPHcLnHfkPzusThqbpyfqnHm0uHdCIZwsT1CEQLILIz4%5FmyIEIi4WUvYEUA78uA-8uzdsmyI-QLKWQLP-mgFWpa4CIAd%5F5LNYUNq1ULNzmvRqUNqWu-qWTZwxmh7GuZNxTAPBI0KWThnqPHfdrHD%26tpl%3Dtpl%5F11534%5F22672%5F17382%26l%3D1518133990%26ie%3Dutf-8%26f%3D8%26tn%3Dbaidu%26wd%3D%25E6%258B%2589%25E5%258B%25BE%25E7%25BD%2591%26oq%3D%2525E7%25258A%2525B6%2525E6%252580%252581%2525E7%2525A0%252581418%26rqlang%3Dcn%26inputT%3D1425; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Flanding-page%2Fpc%2Fsearch.html%3Futm%5Fsource%3Dm%5Fcf%5Fcpt%5Fbaidu%5Fpcbt; _gid=GA1.2.460935757.1591105041; gate_login_token=79d595a531cb883234cf35e3eb7bc7d0be9bda684cd7f071822d51a8588720ef; LG_LOGIN_USER_ID=89be474a3e1cee1aae33927802ba5e5818ab214e7fbadf94e283499c55e1de4f; LG_HAS_LOGIN=1; _putrc=017C13DA374A3409123F89F2B170EADC; JSESSIONID=ABAAAECAAEBABII74FC546A218A4DFA46F6A673583901CA; login=true; unick=%E6%9C%B1%E6%B5%A9; showExpriedIndex=1; showExpriedCompanyHome=1; showExpriedMyPublish=1; hasDeliver=181; privacyPolicyPopup=false; index_location_city=%E6%B7%B1%E5%9C%B3; WEBTJ-ID=20200602213752-1727540ac3c675-067be0efbdb8b-f313f6d-1024000-1727540ac3da6e; RECOMMEND_TIP=true; SEARCH_ID=f44b0aae8e38452d8400e9b60dbda8b3; X_HTTP_TOKEN=d1f77bd533b02b880625011951495ecab60b51a04b; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1591105216; TG-TRACK-CODE=search_code; LGRID=20200602214245-2805a503-2b03-4e4b-9d82-075016abd8af‘, ‘refer‘:‘https://www.lagou.com/jobs/list_%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95?labelWords=&fromSearch=true&suginput=‘, ‘user-agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36‘ } result = requests.post(url=‘https://www.lagou.com/jobs/positionAjax.json?city=%E6%B7%B1%E5%9C%B3&needAddtionalResult=false‘,data=data) print(result.status_code) print(result.text)
------------恢复内容结束------------
以上是关于request库的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段