urllib运用(2)
2021-04-02 17:26
标签:inpu use base agent fir color 查询 write mozilla urllib运用(2) 标签:inpu use base agent fir color 查询 write mozilla 原文地址:https://www.cnblogs.com/lizhihoublog/p/11206785.html# 使用parse对网址进行拼接操作
from urllib import request,parse
base_url = ‘https://www.baidu.com/s?‘
wd = input(‘请输入查询信息>>‘)
query_msg = {
‘wd‘:wd
}
msg = parse.urlencode(query_msg)
url = base_url+msg
header = {
‘User-Agent‘:‘Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0‘
}
req = request.Request(url=url,headers=header)
response = request.urlopen(req).read().decode(‘utf-8‘)
with open(‘teiba.html‘,‘w‘,encoding=‘utf-8‘)as files:
files.write(response)