Python爬虫(6) --深层爬取
创始人
2024-12-01 10:05:14
0

深层爬取

在前面几篇的内容中,我们都是爬取网页表面的信息,这次我们通过表层内容,深度爬取内部数据。

接着按照之前的步骤,我们先访问表层页面:

  1. 指定url
  2. 发送请求
  3. 获取你想要的数据
  4. 数据解析

我们试着将以下豆瓣读书页面的书籍进一步爬取:

https://book.douban.com/tag/%E4%BA%92%E8%81%94%E7%BD%91 

在这里插入图片描述

在浏览器点击这本书,我们要通过这个页面进去这本书的详细页面爬取它的详细信息,它的详细页面链接在href标签中。

爬取

指定url

url = "https://book.douban.com/tag/%E4%BA%92%E8%81%94%E7%BD%91" 

发送请求

import fake_useragent import requests head = {     "User-Agent": fake_useragent.UserAgent().random } resp = requests.get(url, headers=head) 

获取想要的数据

from lxml import etree res_text = resp.text 

数据解析

tree = etree.HTML(res_text) a_list = tree.xpath("//ul[@class='subject-list']/li/div[2]/h2/a") 

定位到位置之后,我们要取到具体的href标签中的链接,再次进行访问请求:

在这里插入图片描述

进入深层

爬取这个详细页面的内容:

for a in a_list:     # 1、url     book_url = "".join(a.xpath("./@href"))     # 2、发送请求     book_res = requests.get(book_url, headers=head)     # 3、获取想要的信息     book_text = book_res.text     # 4、数据解析     book_tree = etree.HTML(book_text)      book_name = "".join(book_tree.xpath("//span[@property='v:itemreviewed']/text()"))      author = "".join(book_tree.xpath("//div[@class='subject clearfix']/div[2]/span[1]/a/text()"))     publish = "".join(book_tree.xpath("//div[@class='subject clearfix']/div[2]/a[1]/text()"))     y = "".join(book_tree.xpath("//span[@class='pl' and text()='出版年:']/following-sibling::text()[1]"))     page = "".join(book_tree.xpath("//span[@class='pl' and text()='页数:']/following-sibling::text()[1]"))     price = "".join(book_tree.xpath("//span[@class='pl' and text()='定价:']/following-sibling::text()[1]"))     bind = "".join(book_tree.xpath("//span[@class='pl' and text()='装帧:']/following-sibling::text()[1]"))     isbn = "".join(book_tree.xpath("//span[@class='pl' and text()='ISBN:']/following-sibling::text()[1]"))      

完整代码显示

# 通过表层内容 深度爬取内部数据 import time import fake_useragent import requests from lxml import etree  head = {     "User-Agent": fake_useragent.UserAgent().random }  if __name__ == '__main__':      # 1、url     url = "https://book.douban.com/tag/%E4%BA%92%E8%81%94%E7%BD%91"      # 2、发送请求     resp = requests.get(url, headers=head)     time.sleep(5)  #请求时停留5秒,不然请求太快可能会被网页拒绝      # 3、获取想要的数据     res_text = resp.text     # print(res_text)      # 4、数据解析     tree = etree.HTML(res_text)      a_list = tree.xpath("//ul[@class='subject-list']/li/div[2]/h2/a")      for a in a_list:         time.sleep(3)         # 1、url         book_url = "".join(a.xpath("./@href"))         # 2、发送请求         book_res = requests.get(book_url, headers=head)         # 3、获取想要的信息         book_text = book_res.text         # 4、数据解析         book_tree = etree.HTML(book_text)          book_name = "".join(book_tree.xpath("//span[@property='v:itemreviewed']/text()"))          author = "".join(book_tree.xpath("//div[@class='subject clearfix']/div[2]/span[1]/a/text()"))         publish = "".join(book_tree.xpath("//div[@class='subject clearfix']/div[2]/a[1]/text()"))         y = "".join(book_tree.xpath("//span[@class='pl' and text()='出版年:']/following-sibling::text()[1]"))         page = "".join(book_tree.xpath("//span[@class='pl' and text()='页数:']/following-sibling::text()[1]"))         price = "".join(book_tree.xpath("//span[@class='pl' and text()='定价:']/following-sibling::text()[1]"))         bind = "".join(book_tree.xpath("//span[@class='pl' and text()='装帧:']/following-sibling::text()[1]"))         isbn = "".join(book_tree.xpath("//span[@class='pl' and text()='ISBN:']/following-sibling::text()[1]"))         print(book_name, author, publish, y, page, price, bind, isbn)      # print(a_list)      pass 

总结

其实与爬取视频的操作相差不大,先定位页面位置,再找到深层页面的链接,获取想要的信息。

相关内容

热门资讯

科普!微信炸金花房卡链接怎么弄... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
分享!微信里面炸金花链接房卡/... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
金花房卡购买联系方式/金花斗牛... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
重大通报,牛牛房卡制作链接人人... 人人大厅房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
ia攻略/金花房卡制作链接新西... ia攻略/金花房卡制作链接新西部/新鸿狐大厅/微信链接房卡销售购买新西部/新鸿狐大厅是一款非常受欢迎...
终于发现!微信里面拼三张房卡哪... 微信游戏中心:拼三张房卡,添加微信【66336574】,进入游戏中心或相关小程序,搜索“微信拼三张房...
正规平台有哪些,牛牛房卡怎么购... 蛮王大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
科普!微信炸金花房卡链接房卡,... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
IA解析/斗牛房间怎么创建的新... IA解析/斗牛房间怎么创建的新荣耀/飞鹰互娱/微信链接房间卡怎么购买Sa9Ix苹果iPhone 17...
正版授权“金花房卡微信链接怎么... 悠悠大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
IA解析/金花房卡专卖店新九天... IA解析/金花房卡专卖店新九天大厅//全网房卡低价售新九天大厅是一款非常受欢迎的游戏,咨询房/卡添加...
科普!微信里炸金花房卡卖家联系... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
终于发现!微信里玩炸金花房卡在... 微信游戏中心:炸金花房卡,添加微信【71319951】,进入游戏中心或相关小程序,搜索“微信炸金花房...
分享!微信牛牛房卡链接去哪里买... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
推荐一款!金花房卡出售火星大厅... 微信游戏中心:火星大厅/新道游房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或...
IA解析/金花房卡批发玄灵大厅... IA解析/金花房卡批发玄灵大厅/上游房卡多少钱一张Sa9Ix苹果iPhone 17手机即将进入量产阶...
科普!微信炸金花房卡找谁买,拼... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
微信炸金花链接怎样弄/在哪里买... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
终于发现!微信里面斗牛房卡在哪... 微信游戏中心:斗牛房卡,添加微信【56001354】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”...
科技实测!金花房卡出售星驰娱乐... 星驰娱乐房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...