第2课-request

一、request.get()方法

import requests

headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"Cookie": "BIDUPSID=AF6362F0DD05057A45BD8858ADA20E24; PSTM=1573199332; BAIDUID=AF6362F0DD05057AA477F61DE117576A:FG=1; BD_UPN=12314753; BDUSS=FZydGRqLWZtMUc1MXotNm5KWHRWUmFOWEcwcWk1YXZUOE9JdHQweUN4cEVXdk5kSVFBQUFBJCQAAAAAAAAAAAEAAABF-p4rxKb0ybn-wt4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAETNy11EzctdU; cflag=13%3A3; delPer=0; BD_CK_SAM=1; PSINO=3; BD_HOME=1; BDSFRCVID=Iu8sJeCCxG3eOF6wYxrrQHvis5gyEJpvVZxM3J; H_BDCLCKID_SF=JbAjoKK5tKvbfP0kh-QJhnQH-UnLqh3gb67Z0lOnMp05eq7DjRJZ0xv0QaQ054uq5554atJKQfcW8DO_e6t5D5J0jN-s-bbfHDJK0b7aHJOoDDvNW635y4LdLp7xJ-Q93JcfhxodK-b4jJ6R3-JBQfIQ5RjzhUueWJLfoKtbJC05bP365ITS-t-e5eT22-usW6IOQhcH0hOWsIONXUD-5pFbKl5IBn50-bTMXl5F-PQ4o4nEDUC0-nDSHH-Ot5LO3J; rsv_jmp_slow=1574857111216; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1452_21116_29699_22157; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_645EC=2d9cL9N6vtyFKJ0dqI4rkIyztw14RElULMFDhXCcUXNOnd1eFE7kt7N3E44ZXlSWcOgG; BDSVRTM=131; COOKIE_SESSION=29115_19_7_9_7_2_0_0_7_2_1_0_6920_0_0_0_1574852258_1574844732_1575020644%7C9%2323540_16_1574844726%7C4"

}

params = {'wd':'足球'}
response = requests.get("https://www.baidu.com/s",headers = headers,params=params)
print(response.url)
with open("baidu.html","w",encoding="utf-8") as fp:
fp.write(response.content.decode('utf-8'))

二、request.post()方法

import requests

headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"Referer": "https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput=",
"Cookie": "user_trace_token=20191127101948-2eb2246f-81b2-43bd-bcda-8e20ceae1b67; _ga=GA1.2.572013949.1574821191; LGUID=20191127101949-635172f2-10bc-11ea-a97f-525400f775ce; JSESSIONID=ABAAABAAAIAACBIF1B332825B9749FBD43664DAFDE0EF5B; WEBTJ-ID=20191127102048-16eaaa93f69903-0d593c90a189ea-2393f61-1446808-16eaaa93f6aad4; _gid=GA1.2.1686276845.1574821250; index_location_city=%E5%85%A8%E5%9B%BD; TG-TRACK-CODE=index_search; sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2216eaaa969c05b6-0bf62aabb462f5-2393f61-1446808-16eaaa969c18dd%22%2C%22%24device_id%22%3A%2216eaaa969c05b6-0bf62aabb462f5-2393f61-1446808-16eaaa969c18dd%22%7D; _gat=1; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1574821191,1574821245,1574821819,1574823191; PRE_UTM=m_cf_cpc_baidu_pc; PRE_HOST=www.baidu.com; PRE_SITE=https%3A%2F%2Fwww.baidu.com%2Fbaidu.php%3Fsc.000000j0xnZgmfstWHrO26q_APKBWmSP7IxDU5-olUczry0yto1ISQBRqrCiFa0CV-XbLl0wVlIwpg9wLPS_gnRWQRL5RAl0P1L_SyPgD2O1Wqy30tGKlTWWIZY9PfUQFdvcYqCAULZKFbqxQtNTlOISzGNDtADVHAnfJWeC1a-iBqW5ptC5R30MmLLUlmav6dWudfDcCeq-4E76XwAVImhlUsVy.7R_NR2Ar5Od663r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E9%2592%25A9%26ck%3D2812.23.84.315.144.271.200.304%26shh%3Dwww.baidu.com%26sht%3Dbaiduhome_pg%26bc%3D110101%26us%3D1.1371610.4.0.1.302.0.0; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Flanding-page%2Fpc%2Fsearch.html%3Futm_source%3Dm_cf_cpc_baidu_pc%26m_kw%3Dbaidu_cpc_sh_94c1f8_375e26_%253B%25E6%258B%2589%25E9%2592%25A9; LGSID=20191127105308-0b0d17c8-10c1-11ea-a981-525400f775ce; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1574823208; LGRID=20191127105326-159a1887-10c1-11ea-a684-5254005c3644; X_HTTP_TOKEN=335c85cd8d161f4e2123284751a654eaa9e075f11b; X_MIDDLE_TOKEN=b5712555a230cfe96e517439c6aeb289; SEARCH_ID=b6ca5ac0bcc44f2b8f77bf5c0f2bec17",
}

data = {
'first':'true',
'pn':1,
'kd':'python'
}
url = "https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false"

response = requests.post(url=url,data=data,headers= headers)

print(type(response.json()))

三、request使用代理proxy


#   常用免费代理  1、西刺免费代理IP:http://www.xicidaili.com  2、快代理:http://www.kuaidaili.com  3、代理云:http://www.dailiyun.com
import requests
#使用代理
proxy = {
'http':"136.228.128.14:61158"
}

url = "http://httpbin.org/ip"
response = requests.get(url=url,proxies=proxy)
print(response.text)

四、request建立会话session


import requests

# response = requests.get("http://www.baidu.com")
#
# print(response.cookies.get_dict())

#request建立会话
session = requests.session()


login_url = "http://172.18.254.102:8080/IBSManage/loginAction_login.action"

headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
"Referer": "http://172.18.254.102:8080/IBSManage/login.jsp"
}

data = {
"curuser.username": "yaoy",
"curuser.password": "sand"
}

session.post(url=login_url,headers=headers,data=data)


dapeng_url = "http://172.18.254.102:8080/IBSManage/trans_list.action"

trans_data = {
"startDate":"2019-11-27",
"endDate": "2019-11-27",
"page": 1,
"rows": 15
}
response = session.post(url=dapeng_url,data=trans_data,headers=headers)
print(response.json())
posted @ 2019-12-02 13:38  高文祥888888  阅读(1582)  评论(0编辑  收藏  举报