我试图从这个URL https://eksisozluk.com/mortingen-sitraze--1277239中提取数据,我想提取标题,然后是标题下的所有注释。如果你打开网站,你会看到标题下的第一条评论是(bkz:mortingen)。问题是,(bkz位于div中,而div中mortingen位于锚定链接中,因此很难按照Web站点上显示的方式刮取数据。有谁能帮助我使用CSS选择器或Xpath来刮取所有注释,如图所示。 我的代码写在下面,但它给了我(bkz:在一列中,然后akhisar,然后)在三个独立的列中,而不是一列中
def parse(self, response):
data={}
#count=0
title = response.css('[itemprop="name"]::text').get()
#data["Title"] = title
count=0
data["title"] = title
count=0
for content in response.css('li .content ::text'):
text = content.get()
text=text.strip()
content = "content" +str(count)
data[content] = text
count=count+1
yield data
您应该首先获得所有不带
::text
的.content
,并使用for
-loop分别处理每个.content
。对于每个.content
,您应该运行::text
以仅获取此内容中的所有文本,将其放入列表中,然后将其合并为单个字符串最小工作代码
您可以将所有代码放在一个文件中并运行
python script.py
,而无需在scrapy
中创建项目编辑:
用
getall()
稍微短一点相关问题 更多 >
编程相关推荐