首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >抓取隐藏文本(?)在javascript对象后面

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

Stack Overflow用户
提问于 2015-06-30 10:14:36
回答 1查看 1.2K关注 0票数 1

我使用刮除,我想提取文本元素。这是我想刮-vitodens-222-f-13-kw-viessmann.html的网页

我使用以下xpath命令:

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

在html代码中有一些产品(表中的行),这些产品工作得很好。但是,在某些情况下,javascript直接嵌入在文本之前:

代码语言:javascript
复制
<td class="title">
  <a class="offer-title link-2 webtrekk wt-prompt" ... >
    <script type="text/javascript"> ... </script>
    text I need 
  </a>
</td>

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

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

代码语言:javascript
复制
        # 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()

但我总是失败。

我很乐意得到任何帮助。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-30 14:02:52

看起来,对于那些存在<script>标记的单元格,HTML节点中没有文本。从对它们的JavaScript的快速检查(碰巧是不受限制的)来看,这些单元似乎在JS运行时被填充了文本。所以你不会发疯的,那些细胞里肯定没有任何短信。

要获得该文本,您需要遵循链接并从下一页获取标题(由于并非每个链接都指向同一个站点),或者您需要使用一些JS引擎(如Selenium (pip install selenium))来获取标题(这在某种程度上必须是有条件的):

代码语言:javascript
复制
>>> 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发出多个请求,但我将留给您;)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31135541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档