我使用刮除,我想提取文本元素。这是我想刮-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()但我总是失败。
我很乐意得到任何帮助。谢谢。
发布于 2015-06-30 14:02:52
看起来,对于那些存在<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
Viessmann Vitodens 222-F Kompakt-Brennwerttherme, 13 kW, VT100, HE ohne Abgaspaket, ohne Anschluss-Set Viessmann
Viessmann Vitodens 222-F wahlweise 13,19, 26 oder 35 kW + Vitotronic 100 oder 200 (Regelung: Vitotronic 100, max. Wärmeleistung (KW): 13)
Paket Vitodens 222-W 13KW mit Vitotronic 200, Ladespeicher und Montagehilfe AP
Viessmann Vitodens 222-F nach Wahl, 13, 19 & 26 kW, Gas-Brennwert-Kompaktgeräte (Abgaspaket: Ohne, Anschluss-Set: Ohne, Regelung: Vitotronic 100, Heizkreispumpe: Hocheffizient, Leistung: 13kW)
Vitodens 222-F 13 kW mit Vitotronic 100 HC1B, hocheffizient
Viessmann Paket Vitodens 222-F 13 kW Vitotronic
Viessmann Paket Vitodens 222-F 13 kW Vitotronic
Viessmann Vitodens 222-F B2TA mit Vitotronic 100 3,2 - 13,0 kW
Viessmann Vitodens 222-F B2SA mit Vitotronic 100 3,2 - 13,0 kW
Viessmann 222-F Gastherme, 13 kW, B2SA010, Speicher innenbeheizt, Aufputz l/r Viessmann
Viessmann 222-F Gastherme, 13 kW, B2SA007, Speicher innenbeheizt, Aufputz oben Viessmann
Vitodens 222-F mit Vitotronic 200, Ladespeicher 3,2 - 13,0 kW
Viessmann Vitodens 222-F BS2A mit Vitotronic 200 3,2 - 13,0 kW
Vitodens 222-F 13KW mit Speicher mit Vitotronic 200 Kompaktgerät
Vitotronic 200 HO1B, Montagehilfe AP 3,2 - 13,0 kW, Aufputz-Montage然后你就有了。15结果。
注意:这个功能在下载中间件中显然会更好,这样就不会对同一个URL发出多个请求,但我将留给您;)
https://stackoverflow.com/questions/31135541
复制相似问题