#模拟百度搜索,爬取搜索网页#例如:https://www.baidu.com/s?ie=UTF-8&wd=%E6%B5%B7%E8%B4%BC%E7%8E%8B(海贼王)import urllib.request
import urllib.parse
#1.urlencode(字典)
Name={'wd':input('Enter the name that you wanna search: ')}
name=urllib.parse.urlencode(Name)
url='https://www.baidu.com/s?ie=UTF-8&'+name
#2.quote(字符串)
Name=input('Enter the name that you wanna search: ')
name=urllib.parse.quote(Name)
url='https://www.baidu.com/s?ie=UTF-8&wd='+name
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'}
seq=urllib.request.Request(url,headers=headers)
response=urllib.request.urlopen(seq)
html=response.read().decode('utf-8')#写入文件withopen(f'百度搜索{Name['wd']}','w',encoding='utf-8')as f:
f.write(html)