javascript - 抓取隐藏在 javascript 对象后面(?)的文本

标签 javascript python html xpath scrapy

我正在使用 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()

但我总是失败。

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

最佳答案

看起来对于那些 <script> 的单元格来说标签存在,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 发出多个请求,但我会把它留给你;)

关于javascript - 抓取隐藏在 javascript 对象后面(?)的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31135541/

相关文章:

Python - 我读取了一个文件,但它显示了错误的值?

PHP 脚本似乎没有获取变量值

javascript - HTML javascript 从输入中获取值

javascript - Selenium 中的 execute_script() 有什么作用

java - Jython 将 java File 对象数组从 python 发送到 java 类

javascript - 通过单击同一 div 中的按钮动态选择元素

javascript - 导航时如何保持 iOS Safari 的独立模式

javascript - Webpack 库输出

javascript - Chrome 分机 : Javascript interrogate URL string for hostname

JavaScript在html5输出标签中显示html文本输入