当前位置: 首页 > >

根据输入内容自动转到浏览器并搜索

发布时间:

import urllib.request
import urllib.parse


headers = {
‘User-Agent’: ’ Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36’}
base_url = ‘https://baike.baidu.com/item/’
kw = input(‘请输入你要搜索的内容:’)
kw = urllib.parse.quote(kw)
url = base_url + kw
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
html = response.read().decode(‘utf-8’)
print(url)
with open(r’E:pythonProjectTestdemo_01.txt’, ‘w’,encoding = ‘utf-8’) as f:
f.write(html)
#见下,爬取的内容自动存入demo_01.txt中



友情链接: