我在试着跟视频学习python爬取并下载贴吧的实战的时候,我发现自己下载好了之后,点开以后显示是这个样子的,一直在加载,在加载好还是出不来,希望各位看一下
from urllib import request
import urllib
import time
#构造请求头信息
header={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62",
"cokie":"BAIDUID_BFESS=C21E87F3A6141FEB572318C6401DB7F7:FG=1; __yjs_duid=1_f40a3356560e7cb76db7f507636f39241627698973259; BIDUPSID=D0964D17E35A3545AC619ADA558551F9; PSTM=1627874092; BAIDUID=D0964D17E35A35450EB2B011CB5E575B:FG=1; ab_sr=1.0.1_ZTNiYTdjNjVmNjA2NTZlYWNkODZhZjBmNmMxMzJlZWUxZGJiMGFhZTBmN2M3N2EzNjAyMTdlMGM4MDIxNjA2ZGQ4NDRkNzNhMmZmMmM2NTFlMDFiYTBhMGEyYWYzNTYwZjY3ZDg5NzE0MDQwMDVmZjNlM2M1NDQ1ZWIzMTVmZWJhYjU5Y2MwMDA2M2M5YmFhNmExMWRhY2FhYTVlODhlNw==",
}
#https://tieba.baidu.com/f?kw=python&ie=utf-8 第一页
#https://tieba.baidu.com/f?kw=python&ie=utf-8&pn=0 第一页
#https://tieba.baidu.com/f?kw=python&ie=utf-8&pn=50 第二页
#https://tieba.baidu.com/f?kw=python&ie=utf-8&pn=100 第三页
def loadpage(fullurl,filname):
print("正在下载:",filname)
req=request.Request(fullurl,headers=header)
response=request.urlopen(req).read()
return response
def writepage(html,filname):
print("正在保存:",filname)
with open(filname,"wb") as f:
f.write(html)
print("-------------------")
#构造url
def tiezi(url,begin,end):
for page in range(begin,end+1):
pn=(page-1)*50
fullurl=url+"&pn="+str(pn) #贴吧每一页的网址
filname="D:/第"+str(page)+"页.html" #每次请求保存文件名
html=loadpage(fullurl,filname) #调用爬虫,爬取网页
writepage(html,filname) #把获取的网页信息写入本地
if __name__ == '__main__':
kw=input("请输入贴吧名:")
begin=int(input("请输入起始页:"))
end=int(input("请输入结束页:"))
url="http://tieba.baidu.com/f?"
#构造url编码
key=urllib.parse.urlencode({"kw":kw})
url=url+key
tiezi(url,begin,end)
time.sleep(10)