搜外网>搜外问答>问答页面

为什么下载不了百度贴吧的页面?

代码如下面

from urllib import response
from urllib.request import Request, urlopen
from urllib.parse import urlencode
from fake_useragent import UserAgent
def get_html(url):
headers = {
"User-Agent": UserAgent().chrome
}
request = Request(url, headers=headers)
response = urlopen(request)
print(response.read().decode())
return response.read()
def save_html(filename, html_bytes):
with open(filename, "wb") as f:
f.write(html_bytes)
def main():
content = input("请输入要下载的内容:")
num = input("请输入要下载多少页:")
base_url = "https://tieba.baidu.com/f?&ie=utf-8&{}"
for pn in range(int(num)):
args = {
"pn": pn * 50,
"kw": content
}
filename = "第" + str(pn + 1) + "页.html"
args = urlencode(args)
print("正在下载" + filename)
html_bytes = get_html(base_url.format(args))
save_html(filename, html_bytes)
if __name__ == '__main__':
main()
追加问题
    5 人参与回答

准备干啥

此后故乡只有冬
此后故乡只 · 爱云查

你这是准备干啥

___ Alone丶
___ A · SEO最高境界——佛系SEO!

下载?你要下载什么?抓取内容?

陈知行 | 生涯规划师
陈知行 | · 高考志愿填报,青少年生涯规划,学业规划指导,心理辅导

下载百度贴吧,是要抓取信息?