#coding=utf-8 #Version:python3.7.4 #Tools:Pycharm 2018.3.5 _date_ = '2020/7/13 23:42 ' import urllib.request import urllib.parse url="https://tieba.baidu.com/f" name=str(input("请输入贴吧名称")) #将贴吧名称变为url码 word=urllib.parse.quote(name) num1=int(input("请输入开始页面数")) num2=int(input("请输入结束页面数")) ua_headers= {"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"} for i in(num1,num2+1): #贴吧每也pn要加上50,首页为0 num=(i-1)*50 page=str(num) #完整url newUrl = url + "?kw=" + word+"&pn="+page print(newUrl) req=urllib.request.Request(newUrl,headers=ua_headers) req1=urllib.request.urlopen(req) html=req1.read().decode() print(html) print("--------%d打印完毕---------------",i)