Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在scrapy中提取em标记中的文本_Python_Scrapy - Fatal编程技术网

Python 在scrapy中提取em标记中的文本

Python 在scrapy中提取em标记中的文本,python,scrapy,Python,Scrapy,因此,我在刮壳中,我尝试的一个选择器产生以下结果: >>> response.css(".result-title a").extract() [u'<a href="#"><em>Membership warehouse</em></a>', u'<a href="#">Publix</a>', u'<a href="#">Kroger Pharmacy</a>'] 很明显,这省

因此,我在刮壳中,我尝试的一个选择器产生以下结果:

>>> response.css(".result-title a").extract()
[u'<a href="#"><em>Membership warehouse</em></a>', u'<a href="#">Publix</a>', u'<a href="#">Kroger Pharmacy</a>']
很明显,这省略了第一个元素,该元素在文本周围有一个额外的em标记。如何将其与普通文本一起提取,以便最终输出为:

[u'Membership warehouse', u'Publix', u'Kroger Pharmacy']

我会在
a
中找到任何级别的所有文本节点,并“连接”它们:

演示:

$cat index.html
$scrapy shell index.html
In[1]:对于In response.css(“.result title a”):
print(“.join(a.xpath(“.//text()”).extract())
...:     
会员仓库
Publix
克罗格药房
[u'Membership warehouse', u'Publix', u'Kroger Pharmacy']
for a in response.css(".result-title a"):
     print("".join(a.xpath(".//text()").extract()))
$ cat index.html 
<div class="result-title">
    <a href="#"><em>Membership warehouse</em></a>
    <a href="#">Publix</a>
    <a href="#">Kroger Pharmacy</a>
</div>
$ scrapy shell index.html
In [1]: for a in response.css(".result-title a"):
    print("".join(a.xpath(".//text()").extract()))
   ...:     
Membership warehouse
Publix
Kroger Pharmacy