【问题标题】:Scrapy xpath not extracting div containing special characters <%=Scrapy xpath 不提取包含特殊字符的 div <%=
【发布时间】:2021-01-09 09:54:28
【问题描述】:

我是 Scrapy 的新手。我正在尝试从以下 URL 中提取 h2 文本:'https://www.tysonprop.co.za/agents/'

我有两个问题:

  1. 我的 xpath 可以到达 script 元素,但在 script 标签内找不到 h2 或 div 元素。我什至尝试将 HTML 文件保存到我的机器并抓取该文件,但同样的问题发生了。我已经三次检查了我的 xpath 代码,一切似乎都井井有条。

  2. 当网站显示在我的浏览器中时,branch.branch_name 解析为“Tysen Properties Head Office”。如何获取值(即“Tysen Properties Head Office”)而不是变量名(branch.branch_name)?

我的 Python 代码:

import scrapy

class TysonSpider(scrapy.Spider):
    name = 'tyson_spider'

    def start_requests(self):
        url = 'https://www.tysonprop.co.za/agents/'
        yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):

        script = response.xpath('//script[@id="id_branch_template"]')
        div = script.xpath('./div[contains(@class,"branch-container")]')
        h2 = div.xpath('/h2[contains(@class,"branch-name")]/text()').extract()
        yield {'branchName': h2}

HTML 摘录如下:

<script type="text/html" id="id_branch_template">
  <div id="branch-<%= branch.id %>" class="clearfix margin-top30 branch-container" style="display: none;">
    <h2 class="grid_12 branch-name margin-bottom20"><%= branch.branch_name %></h2>
    <div class="branch-agents container_12 first last clearfix">
      <div id="agents-list-left" class="agents-list left grid_6">
      </div>
      <div id="agents-list-right" class="agents-list right grid_6">
      </div>
    </div>
  </div>
</script>

【问题讨论】:

  • HTML 代码为:script type="text/html" id="id_branch_template">

标签: javascript python html scrapy


【解决方案1】:

script 标签内的div 是一个文本。 要将其获取为 html,您可以执行以下操作:

from scrapy.selector import Selector

....
def parse(self, response):

        script = Selector(text=response.xpath('//script[@id="id_branch_template"]/text()').get())
        div = script.xpath('./div[contains(@class,"branch-container")]')
        h2 = div.xpath('.//h2[contains(@class,"branch-name")]/text()').extract()
        yield {'branchName': h2}

但请注意,h2 不包含任何文本,因此您的结果将是一个空数组

【讨论】:

  • 谢谢,这肯定有帮助!如何获得 h2 标题的 部分?在我的浏览器中查看站点时,branch.branch_name 解析为一个值 - 是否可以提取此值?
  • 要提取您必须使用seleniumsplash 的值,或者您可以检查chrome 上的网络选项卡并尝试在那里找到该值,我想该信息来自AJAX 之一打电话。
  • 谢谢@Moein Kameli,这正是我需要的帮助。衷心感谢。
【解决方案2】:

branch.branch_name 看起来像 JSON 格式的地址吗?是否有呼叫加载您正在寻找的数据?也许,让我们看看

通过浏览您的浏览器开发工具,您可以在网络选项卡中找到请求,通过在它们之间进行搜索,您将看到this AJAX call,它会准确加载您正在寻找的数据。所以:

import json
import scrapy
class TysonSpider(scrapy.Spider):
    name = 'tyson_spider'
    def start_requests(self):
        url = 'https://www.tysonprop.co.za/ajax/agents/?branch_id=25'
        yield scrapy.Request(url=url, callback=self.parse)
    def parse(self, response):
        json_data = json.loads(response.text)
        branch_name = json_data['branch']['branch_name']
        yield {'branchName': branch_name}

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 2020-03-17
    • 2015-05-15
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多