【发布时间】:2019-04-16 02:28:28
【问题描述】:
在这被标记为重复之前,我已经搜索并尝试了在 SO 上找到的其他解决方案,它们是:
- scrapy css selector: get text of all inner tags
- How to get the text from child nodes if it is parents to other node in Scrapy using XPath
- scrapy get the entire text including children
我要提取的 HTML 是:
<span class="location">
Mandarin Oriental Hotel
<a class="" href="/search-results/Jalan+Pinang%252C+Kuala+Lumpur+City+Centre%252C+50088+Kuala+Lumpur%252C+Wilayah+Persekutuan./?state=Kuala+Lumpur" itemprop="addressRegion" title="Jalan Pinang, Kuala Lumpur City Centre, 50088 Kuala Lumpur, Wilayah Persekutuan.">
Jalan Pinang, Kuala Lumpur City Centre, 50088 Kuala Lumpur, Wilayah Persekutuan.
</a>
,
<a class="" href="/search-results/?neighbourhood=Kuala+Lumpur&state=Kuala+Lumpur" title="Kuala Lumpur">
Kuala Lumpur
</a>
,
<a class="" href="/search-results/?state=Kuala+Lumpur" title="Kuala Lumpur">
Kuala Lumpur
</a>
<span class="" itemprop="postalCode">
50088
</span>
</span>
我想获取 //span[@class='location'] 中的所有文本。
我试过了:
response.xpath("//span[@class='location']//text()").extract_first()response.css("span.location *::text").extract_first()response.css("span.location ::text").extract_first()
他们都只返回Mandarin Oriental Hotel,而不是完整的地址。
编辑: 文本应该产生
Mandarin Oriental Hotel Jalan Pinang, Kuala Lumpur City Centre, 50088 Kuala Lumpur, Wilayah Persekutuan., Kuala Lumpur, Kuala Lumpur 50088
【问题讨论】:
-
我不是 Scrapy 用户,但我想这是因为您使用的是
extract_first。试试" ".join(response.xpath("//span[@class='location']//text()").extract()) -
@Andersson 不幸的是,这将产生页面中所有单个项目的地址。页面:hungrygowhere.my/search-results/?search_location=Kuala+Lumpur
-
您的意思是将所有地址作为单个字符串返回,并且您希望每个结果都有单独的地址?
标签: python xpath web-scraping scrapy