Python爬虫(6) --深层爬取

深层爬取

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

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

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

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

python 复制代码
https://book.douban.com/tag/%E4%BA%92%E8%81%94%E7%BD%91

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

爬取

指定url

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

发送请求

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

获取想要的数据

python 复制代码
from lxml import etree
res_text = resp.text

数据解析

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

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

进入深层

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

python 复制代码
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]"))
    

完整代码显示

python 复制代码
# 通过表层内容 深度爬取内部数据
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

总结

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

相关推荐
Sylvia-girl2 小时前
Java——抽象类
java·开发语言
Yana.nice4 小时前
Bash函数详解
开发语言·chrome·bash
江沉晚呤时4 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
电脑能手5 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
tomorrow.hello5 小时前
Java并发测试工具
java·开发语言·测试工具
Edward-tan5 小时前
CCPD 车牌数据集提取标注,并转为标准 YOLO 格式
python
晓13136 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊6 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
倔强青铜三6 小时前
苦练Python第18天:Python异常处理锦囊
人工智能·python·面试
倔强青铜三6 小时前
苦练Python第17天:你必须掌握的Python内置函数
人工智能·python·面试