# coding utf-8
from urllib.request import Request,urlopen
from urllib.parse import urlencode
def get_html(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
}
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()
贴吧案例
最新推荐文章于 2024-07-16 00:58:32 发布