“”"
@theme 爬虫
@time 2018/12/16
@author lz
@content 爬取python吧的多个页面
@step
1导入2发出请求3转码4保存
@analysis
第一个页面 http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=0
第2个页面 http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=50
第3个页面 http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=100
第n个页面 http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=(n-1)*50
“”"
#1导入网络模块
from urllib import request
url=“http://tieba.baidu.com/f?kw=python&ie=utf-8&pn=”
def getContent(url,page):#因为访问多个页面,所以要多次调用
#2发送网络请求
response=request.urlopen(url)
#3转码
content=response.read().decode(“utf-8”)
#4保存
name=str(page)+".html"
with open (name,“w”,encoding=“utf-8”) as fp:
fp.write(content)
#利用爬虫爬取10个页面
for page in range(1,11,1):
pn=(page-1)*50
full_url=url+str(pn)
print(full_url)
getContent(url,page)
机器学习中python爬虫爬取百度贴吧多个数据
最新推荐文章于 2022-08-29 17:51:08 发布