天天看點

爬蟲基礎執行個體 Requests _post請求

import requests
import json
url = "https://fanyi.baidu.com/sug"

# 攜帶的參數形式
params = {
    "kw":"美女"
}
# 僞裝浏覽器
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
}

# 發送請求
response = requests.post(url=url,params=params,headers=headers)

print(response.status_code)
print(response.text)
# {"errno":0,"data":[{"k":"\u7f8e\u5973","v":"[m\u011bi n\u01da] beautiful woman; beauty;"},{"k":"\u7f8e\u5973\u4e0e\u91ce\u517d","v":"Beauty and the Beast; Beauty and Beast; Beauty & T"},{"k":"\u7f8e\u5973\u86c7","v":"\u540d beautiful villain; Hisss; The Snake Woman; Cobra"}]}
page_text = json.loads(response.text)
print(page_text)
# {'errno': 0, 'data': [{'k': '美女', 'v': '[měi nǚ] beautiful woman; beauty;'}, {'k': '美女與野獸', 'v': 'Beauty and the Beast; Beauty and Beast; Beauty & T'}, {'k': '美女蛇', 'v': '名 beautiful villain; Hisss; The Snake Woman; Cobra'}]}
result = page_text["data"][0]["v"][-8:-1]
print(result)    # beauty
           

繼續閱讀