python爬虫实现爬取同一个网站的多页数据的实例讲解
作者:宋宋大人 时间:2021-04-11 13:52:09
对于一个网站的图片、文字音视频等,如果我们一个个的下载,不仅浪费时间,而且很容易出错。Python爬虫帮助我们获取需要的数据,这个数据是可以快速批量的获取。本文小编带领大家通过python爬虫获取获取总页数并更改url的方法,实现爬取同一个网站的多页数据。
一、爬虫的目的
从网上获取对你有需要的数据
二、爬虫过程
1、获取url(网址)。
2、发出请求,获得响应。
3、提取数据。
4、保存数据。
三、爬虫功能
可以快速批量的获取想要的数据,不用手动的一个个下载(图片、文字音视频等)
四、使用python爬虫爬取同一网站多页数据
1、需要定位至该标签并获得总页数
def get_page_size(soup):
pcxt=soup.find('div',{'class':'babynames-term-articles'}).find('nav')
pcxt1=pcxt.find('div',{'class':'nav-links'}).findAll('a')
for i in pcxt1[:-1]:
link=i.get('href')
s=str(i)
page=re.sub('<a href="','',s)
page1=re.sub(link,'',page)
page2=re.sub('">','',page1)
page3=re.sub('</a>','',page2)
pagesize=int(page3)
print(pagesize)
return pagesize
Pass
2、更改url来访问网址,也就是进行主函数的编写
if __name__ == '__main__':
url="http://www.sheknows.com/baby-names/browse/a/"
soup=get_requests(url)
page=get_page_size(soup)
for i in range(1,page+1):
url1=url+"page/"+str(i)+"/"
soup1=get_requests(url1)
draw_base_list(soup1)
实例扩展:
import requests
from lxml import etree
import re
url="https://movie.douban.com/top250"
header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}
allMovieList=[]
flag = True
while flag:
html = requests.get(url, headers=header).text
list = etree.HTML(html)
lis = list.xpath('//ol[@class="grid_view"]/li')
for oneSelector in lis:
name = oneSelector.xpath("div/div[2]/div[1]/a/span[1]/text()")[0]
score = oneSelector.xpath("div/div[2]/div[2]/div/span[2]/text()")[0]
people = oneSelector.xpath("div/div[2]/div[2]/div/span[4]/text()")[0]
people = re.findall("(.*?)人评价",people)[0]
oneMovieList = [name,score,people]
allMovieList.append(oneMovieList)
#获取下一页地址
try:
next_url = list.xpath('//span[@class="next"]/a/@href')[0]
if next_url:
url = "https://movie.douban.com/top250"+ next_url
except:
flag = False
print(allMovieList)
来源:https://www.py.cn/toutiao/23021.html
标签:python爬虫,多页数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解CentOS升级Python2.6到Python2.7并安装pip
2023-06-13 18:59:45
python装饰器decorator介绍
2021-12-18 10:56:25
Python实现名片管理系统
2022-01-11 21:45:12
![](https://img.aspxhome.com/file/2023/9/101469_0s.jpg)
详解OpenCV中直方图,掩膜和直方图均衡化的实现
2022-10-30 12:03:33
![](https://img.aspxhome.com/file/2023/8/107558_0s.jpg)
表单设计中的网页视觉体验
2008-06-26 13:35:00
![](https://img.aspxhome.com/file/UploadPic/20086/26/2008626134221567s.jpg)
Python+Opencv实现表格边框线检测
2023-08-11 04:55:14
![](https://img.aspxhome.com/file/2023/1/61971_0s.png)
python不同版本的_new_不同点总结
2023-09-21 15:36:45
django的登录注册系统的示例代码
2021-05-14 23:27:59
![](https://img.aspxhome.com/file/2023/0/99440_0s.png)
详解python里使用正则表达式的全匹配功能
2023-12-29 17:10:14
Python基于socket实现TCP客户端和服务端
2023-09-29 15:30:05
如何使用Python实现斐波那契数列
2021-12-09 08:20:30
![](https://img.aspxhome.com/file/2023/3/94693_0s.png)
Python calendar模块详情
2023-08-20 23:04:59
![](https://img.aspxhome.com/file/2023/9/67799_0s.png)
NumPy实现多维数组中的线性代数
2021-03-30 09:59:08
![](https://img.aspxhome.com/file/2023/8/89418_0s.png)
Python PyInstaller安装和使用教程详解
2021-05-12 01:28:40
canvas实现手机端用来上传用户头像的代码
2023-09-16 02:30:54
用Python画圣诞树代码示例
2023-06-14 06:22:27
![](https://img.aspxhome.com/file/2023/2/95552_0s.png)
Python中Proxypool库的安装与配置
2022-11-04 15:14:57
![](https://img.aspxhome.com/file/2023/1/77911_0s.png)
PHP采集静态页面并把页面css,img,js保存的方法
2023-10-22 19:44:22
python实现双色球随机选号
2021-06-26 05:11:09
详解Python中的多线程编程
2023-09-17 00:34:08
![](https://img.aspxhome.com/file/2023/1/65131_0s.png)