当前位置: 首页 > 工具软件 > URL Parser > 使用案例 >

urllib.parse的使用

吴修洁
2023-12-01
import urllib.request
import urllib.parse  #解析

# url拼接
url = "https://www.baidu.com/s?"
name = {"wd":"校花"}
new_name = urllib.parse.urlencode(name)
new_url = url + new_name
print(new_url)
# 请求头
headers = {
    'User-Agent':
        'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
        'AppleWebKit/537.36(KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36',
    'Cookie': 'BAIDUID=712E599EE8B2B376F19E36399BD4B7F9:FG=1; BIDUPSID=712E599EE8B2B376F19E36399BD4B7F9; PSTM=1586778054; BD_UPN=12314353; BDUSS=DRVS05BWFM1dDRoTlhkSjFuT2FmMndqfmVHVVpEfi1nLVdCaVZrUC1BRnEtTHRlRVFBQUFBJCQAAAAAAAAAAAEAAAClYNinc2NzZGdzYwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGprlF5qa5Rec; COOKIE_SESSION=10187_0_9_9_25_7_1_0_9_4_11_0_10184_0_13_0_1588075131_0_1588075118%7C9%230_0_1588075118%7C1; BDRCVFR[q9-ujHA1ZQ3]=mk3SLVN4HKm; delPer=0; BD_CK_SAM=1; PSINO=7; H_PS_PSSID=1451_21082_31254_31424_31341_30908_31271_31464_30823_31164_31472; H_PS_645EC=9a46SzUYhYMLeJqkuEBqT8hYvlHv9BEt8ZVpl3Ufs4NX76TYFgOnJo%2B8unA; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDSVRTM=0; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; userFrom=ala'
    }
# 请求响应过程
req = urllib.request.Request(url,headers=headers)
res = urllib.request.urlopen(req)
html = res.read().decode("utf-8")

# 保存到本地
with open("百度校花.html","w",encoding="utf-8") as f :
    f.write(html)

 类似资料: