【问题标题】:scraping text hidden (?) behind javascript object抓取隐藏在javascript对象后面的文本(?)
【发布时间】:2015-09-17 02:07:47
【问题描述】:

我正在使用scrapy,我想提取文本元素。这是我要抓取的网页 http://www.idealo.de/preisvergleich/OffersOfProduct/3131289_-vitodens-222-f-13-kw-viessmann.html

我正在使用以下 xpath 命令:

for sel in response.xpath('//tr'):
sel.xpath('td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/text()').extract()

html 代码中有产品(表中的行)可以正常工作。但是,在某些情况下,javascript 会直接嵌入到文本之前:

<td class="title">
  <a class="offer-title link-2 webtrekk wt-prompt" ... >
    <script type="text/javascript"> ... </script>
    text I need 
  </a>
</td>

在这些情况下,我无法检索“我需要的文本”。

我还搜索并尝试了其他几个 xpath 选项,例如获取所有子节点。这些是我尝试过的变体:

        # item['longtitle'] = sel.xpath('td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/script[@type="text/javascript"]/following-sibling::*').extract()
        # item['longtitle'] = sel.xpath('td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/script[@type="text/javascript"]/node()').extract()
        item['longtitle'] = sel.xpath('td[@class="title"]/text()[0]').extract()
        ## item['longtitle'] = sel.xpath('td[@class="title"]/node()').extract()
        ## item['longtitle'] = sel.xpath('td[@class="title"]/text()').extract()
        ## item['longtitle'] = sel.xpath('td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/node()').extract()
        ## item['longtitle'] = sel.xpath('td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/text()').extract()
        ## item['longtitle'] = sel.xpath('td[@class="title"]/a[2]').extract()
        ## item['longtitle'] = sel.xpath('td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/*').extract()
        ## item['longtitle'] = sel.xpath('td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/script[@type="text/javascript"]/text()').extract()

但我一直失败。

我很乐意提供任何帮助。谢谢。

【问题讨论】:

  • //td[@class="title"]/a[@class="offer-title link-2 webtrekk wt-prompt"]/text()[2] 因为
  • 感谢您的及时回复。我尝试了拆分文本的第二个元素,然后是第三个和第四个元素:[2]、[3] 和 [4]。它们都是空的。

标签: javascript python html xpath scrapy


【解决方案1】:

对于那些存在&lt;script&gt; 标签的单元格,HTML 节点中似乎没有文本。通过快速检查他们的 JavaScript(恰好是未缩小的),看起来这些单元格在 JS 运行时填充了文本。所以你不会发疯的,这些单元格中肯定没有任何文字。

要获取该文本,您需要点击链接并从下一页获取标题(这必须是有条件的,因为并非每个链接都指向同一个站点),或者您需要使用一些 JS 引擎来拉取页面,例如 Selenium (pip install selenium):

>>> from selenium import webdriver
>>> my_driver = webdriver.PhantomJS()
>>> my_driver.get(response.url)
>>> results = my_driver.find_elements_by_xpath('//table[contains(@class, "modular")]//tr[.//a]')
>>> for row in results:
...     print row.find_element_by_xpath('./td[@class="title"]/a').text
Viess­mann Vi­to­dens 222-F Kom­pakt-Brenn­wert­ther­me, 13 kW, VT100, HE ohne Ab­gas­pa­ket, ohne An­schluss-Set Viess­mann
Viess­mann Vi­to­dens 222-F wahl­wei­se 13,19, 26 oder 35 kW + Vi­to­tro­nic 100 oder 200 (Re­ge­lung: Vi­to­tro­nic 100, max. Wär­me­leis­tung (KW): 13)
Paket Vi­to­dens 222-W 13KW mit Vi­to­tro­nic 200, La­de­spei­cher und Mon­ta­ge­hil­fe AP
Viess­mann Vi­to­dens 222-F nach Wahl, 13, 19 & 26 kW, Gas-Brenn­wert-Kom­pakt­ge­rä­te (Ab­gas­pa­ket: Ohne, An­schluss-Set: Ohne, Re­ge­lung: Vi­to­tro­nic 100, Heiz­krei­s­pum­pe: Hoch­ef­fi­zi­ent, Leis­tung: 13kW)
Vi­to­dens 222-F 13 kW mit Vi­to­tro­nic 100 HC1B, hoch­ef­fi­zi­ent
Viess­mann Paket Vi­to­dens 222-F 13 kW Vi­to­tro­nic
Viess­mann Paket Vi­to­dens 222-F 13 kW Vi­to­tro­nic
Viess­mann Vi­to­dens 222-F B2TA mit Vi­to­tro­nic 100 3,2 - 13,0 kW
Viess­mann Vi­to­dens 222-F B2SA mit Vi­to­tro­nic 100 3,2 - 13,0 kW
Viess­mann 222-F Gasther­me, 13 kW, B2SA010, Spei­cher in­nen­be­heizt, Auf­putz l/r Viess­mann
Viess­mann 222-F Gasther­me, 13 kW, B2SA007, Spei­cher in­nen­be­heizt, Auf­putz oben Viess­mann
Vi­to­dens 222-F mit Vi­to­tro­nic 200, La­de­spei­cher 3,2 - 13,0 kW
Viess­mann Vi­to­dens 222-F BS2A mit Vi­to­tro­nic 200 3,2 - 13,0 kW
Vi­to­dens 222-F 13KW mit Spei­cher mit Vi­to­tro­nic 200 Kom­pakt­ge­rät
Vi­to­tro­nic 200 HO1B, Mon­ta­ge­hil­fe AP 3,2 - 13,0 kW, Auf­putz-Mon­ta­ge

你有它。 15 个结果。

注意:这个功能在下载器中间件中显然会更好,以免向同一个 URL 发出多个请求,但我会把它留给你;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2014-10-24
    • 2016-02-08
    相关资源
    最近更新 更多