1、requests之get()方法
https://www.cnblogs.com/sruzzg/p/13041898.html 类似
1 """requests之get()方法""" 2 3 import requests 4 5 # response = requests.get('https://www.baidu.com') 6 # 7 # # <class 'str'> 8 # print(type(response.text)) 9 # # print(response.text) 10 # # <class 'bytes'> 11 # print(type(response.content)) 12 # print(response.content.decode('utf-8')) 13 # 14 # print(response.url) 15 # print(response.encoding) 16 # print(response.status_code) 17 18 params = { 19 'q': '中国' 20 } 21 headers = { 22 'user-agent': 'Mozilla/5.0' 23 } 24 25 # https://www.so.com/s?q=%E4%B8%AD%E5%9B%BD 26 respose = requests.get('https://www.so.com/s', 27 params=params, 28 headers=headers) 29 30 print(respose.url) # https://www.so.com/s?q=%E4%B8%AD%E5%9B%BD 31 with open('2_12so.html', 'w', encoding='utf-8') as fp: 32 fp.write(respose.content.decode('utf-8'))
2、requests之post()方法 {例子得到的数据还是有些问题的}
1 """requests之post""" 2 3 import requests 4 5 data = { 6 'first': 'true', 7 'pn': '1', 8 'kd': 'python' 9 } 10 headers = { 11 'user-agent': 'Mozilla/5.0', 12 'referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=', 13 'accept': 'application/json, text/javascript, */*; q=0.01', 14 'cookie': 'user_trace_token=20200517001716-be703f7a-d48b-4638-8595-be07968fc1f5; __guid=237742470.1049823895816539500.1589645836509.809; _ga=GA1.2.18796295.1589645838; RECOMMEND_TIP=true; LGUID=20200517001739-18bc0f6a-e159-4848-adc5-31ddcd27eb1e; index_location_city=%E5%85%A8%E5%9B%BD; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221721e468b5f716-0cd81ae7dc49e4-376b4502-2073600-1721e468b608e7%22%2C%22%24device_id%22%3A%221721e468b5f716-0cd81ae7dc49e4-376b4502-2073600-1721e468b608e7%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%7D%7D; _gid=GA1.2.783209919.1591543462; JSESSIONID=ABAAAECABFAACEA7BF05A1093A5B4613722F93A558CD2D6; WEBTJ-ID=20200608215037-1729432832c29d-0cb63da197a7ec-376b4502-2073600-1729432832d848; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1589812459,1589970166,1591543462,1591624239; TG-TRACK-CODE=index_search; LGSID=20200608222408-febe4e19-487a-4a0d-9945-061b3902e7e0; PRE_UTM=; PRE_HOST=; PRE_SITE=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2F; X_HTTP_TOKEN=0c2c3d1b6f2c08ea05262619513682303dbd47e782; _gat=1; LGRID=20200608222410-209fabf4-657d-4352-b06d-95d031cbbe7d; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1591626250; SEARCH_ID=a57f590dca3d4675bdf6284e5e333da3; monitor_count=6' 15 16 } 17 url = 'https://www.lagou.com/jobs/companyAjax.json?needAddtionalResult=false' 18 response = requests.post(url, 19 data=data, 20 headers=headers) 21 22 print(type(response.text)) # <class 'str'> 23 print(response.text) 24 25 # 把获得到的数据转成字典 26 print(type(response.json())) # <class 'dict'> 27 print(response.json())