簡體   English   中英

Scrapy-xpath中有中文字符時意外返回

[英]Scrapy - unexpected return when there is Chinese character in xpath

我是新來的,我知道有一個類似的問題。 但是,我認為這個問題沒有解決。

我正在使用的scrapy版本是1.0.3,環境在virtualbox中。 我想做的是從中刪除所有信息

https://bj.lianjia.com/zufang/rs%E8%A5%BF%E4%BA%8C%E6%97%97/

@title中包含“西二旗”。 我的腳本是這樣的:

keywords = u'領秀'

response.xpath('//h2/a[contains(@title,keywrods)]/text()').extract()

輸出是這樣的:

[u'\u897f\u4e8c\u65d7\u9886\u79c0\u65b0\u7845\u8c37\u81ea\u4f4f\u578b\u8054\u6392\u522b\u5885', u'\u91d1\u5c71\u8f6f\u4ef6 \u5c0f\u7c73 \u4e94\u5f69\u57ce \u897f\u4e8c\u65d7\u8f6f\u4ef6\u56ed', u'\u9f99\u5174\u56ed\u7cbe\u88c5\u4e24\u5c45\u5ba4\uff0c\u9f99\u6cfd\u56de\u9f99\u89c2\u897f\u4e8c\u65d7\u5317\u6e05\u8def\u3002', u'\u878d\u6cfd\u5609\u56ed\u897f\u4e8c\u65d7\u9f99\u6cfd \u7cbe\u88c5\u4e09\u5c45 \u6708\u5e95\u62ce\u5305\u5165\u4f4f', u'\u9f99\u5174\u56ed\u5317\u533a\u53f2\u8bd7\u7ea7\u7cbe\u88c5\u4fee\u4e24\u5c45\u5ba4\uff0c\u9f99\u6cfd\u56de\u9f99\u89c2\u897f\u4e8c\u65d7\u3002', u'\u4e94\u5f69\u57ce \u5c0f\u7c73 \u91d1\u5c71\u8f6f\u4ef6 \u897f\u4e8c\u65d7\u8f6f\u4ef6\u56ed \u4e0a\u5730\u4e09\u8857', u'\u6b63\u89c4\u5357\u5317\u901a\u900f\u5927\u4e24\u5c45\u6708\u5e95\u5230\u671f\u897f\u4e8c\u65d7\u8f6f\u4ef6\u56ed\u767e\u5ea6', u'\u56de\u9f99\u89c2\u9f99\u6cfd\u897f\u4e8c\u65d7\u5317\u4eac\u4eba\u5bb6\u7cbe\u88c5\u4e24\u5c45\u5bbd\u655e\u660e\u4eae\u62ce\u5305\u4f4f', u'\u897f\u4e8c\u65d7\u9f99\u6cfd\u7535\u68af\u697c\u843d\u5730\u7a97\u5317\u4eac\u4eba\u5bb6\u4e24\u5c45\u7cbe\u88c5\u62ce\u5305\u4f4f']

它將返回所有不包含或不包含關鍵字的元素。

所以我真的很想知道發生了什么。 我還在chrome上使用$x('//h2/a[contains(@title,"領秀")]')嘗試了此操作,並且效果很好(僅返回了一個元素)。

您的代碼並沒有像這樣隱式擴展xpath上的變量:

keywords = u'領秀'
response.xpath('//h2/a[contains(@title,keywrods)]/text()').extract()
                                      ^^^^^^^^^

它應該是:

keywords = u'領秀'
# python 3.6+
response.xpath(f'//h2/a[contains(@title,"{keywrods}")]/text()').extract()
# python 2/3
response.xpath(u'//h2/a[contains(@title,"{}")]/text()'.format(keywords)).extract()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM