# 需求:把只能单一搜索内容换成其他内容
key =input('请输入搜索内容:')
wd ={'wd':key}
result =urllib.parse.urlencode(wd)
base_url ='https://www.baidu.com/s?'
url =base_url +result
# print(url)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
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)
百度贴吧的练习html
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
name = input('请输入贴吧名:')
begin = int(input('请输入起始页'))
end = int(input('请输入结束页'))
kw = {'kw': name}
result = urllib.parse.urlencode(kw)
# 拼接url
for i in range(begin, end + 1):
pn = (i - 1) * 50
print(pn)
base_url = 'https://tieba.baidu.com/f?'
url = base_url + result + '&pn=' + str(pn)
req =urllib.request.Request(url,headers=headers)
res =urllib.request.urlopen(req)
html = res.read().decode('utf-8')
# html = res.read().decode('utf-8')
filename = '第' +str(i) +'页.html'
with open(filename,'w',encoding='utf-8') as f:
print('正在爬取%d页'% i)
f.write(html)