import urllib
import urllib.request
def loadPage(url,filename):
"""
作用:根据url发送请求=,获取服务器响应文件
url:需要爬去的url
filename:处理得文件名
"""
print("正在下载" + filename)
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"}
req = urllib.request.Request(url, headers = headers)
return urllib.request.urlopen(req).read()
def writePage(html,filename):
"""
作用:将HTML内容写入到本地
html:服务器响应得文件内容
"""
print("正在保存" + filename)
#文件写入
with open(filename,"wb") as f:
f.write(html)
print ("_" * 30)
def tiebaSpider(fullurl,beginPage,endPage):
"""
作用:贴吧爬虫调度器,负责处理每个页面得url
url:贴吧url的前部分
beginPage:起始页
endPage:结束页
"""
for page in range(beginPage,endPage + 1):
pn = (page - 1) * 50
filename = "第" + str(page) + "页.html"
print(filename)
fullurl = url + "&pn=" + str(pn)
html = loadPage(fullurl,filename)
writePage(html,filename)
print("谢谢使用")
if __name__ =="__main__":
kw = input("请输入需要爬取得吧名:")
beginPage = int(input("请输入起始页:"))
endPage = int(input("请输入结束页:"))
url = "http://tieba.baidu.com/f?"
key = urllib.parse.urlencode({"kw":kw})
fullurl = url + key
tiebaSpider(fullurl, beginPage, endPage)
网友评论