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

深层爬取

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

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

  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

总结

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

  • 18
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下如何使用 Python 爬虫爬取房产数据并保存到本地。 首先,您需要选择一个合适的网站来进行爬取。例如,可以选择链家网、安居客等房产网站。然后,您需要分析该网站的页面结构和数据请求方式,确定需要爬取哪些数据,以及如何获取这些数据。 接着,您可以使用 Python 的 requests 库来发送 HTTP 请求,获取网页内容。然后,可以使用 BeautifulSoup 库来解析网页内容,提取需要的数据。 最后,您可以使用 Python 的 Pandas 库将数据保存到本地的 Excel 或 CSV 文件中。以下是一个示例代码: ```python import requests from bs4 import BeautifulSoup import pandas as pd # 发送 HTTP 请求获取网页内容 url = 'https://bj.lianjia.com/ershoufang/' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'html.parser') # 解析网页内容,提取需要的数据 house_list = soup.find_all('div', {'class': 'info'}) data = [] for house in house_list: name = house.find('div', {'class': 'title'}).text.strip() price = house.find('div', {'class': 'price'}).text.strip() area = house.find('div', {'class': 'houseInfo'}).text.strip() data.append([name, price, area]) # 将数据保存到本地文件 df = pd.DataFrame(data, columns=['名称', '价格', '面积']) df.to_excel('house_data.xlsx', index=False) ``` 这是一个简单的示例代码,您可以根据您需要爬取的数据和网站的不同来进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值