刮擦信息时列表错误的列表错误
我正在尝试提取信息,但他们会给我不可变的列表错误,这些是链接 https://rejestradwokatow.pl/adwokat/abaewicz-agnieszka-51004
import scrapy
from scrapy.http import Request
from scrapy.crawler import CrawlerProcess
class TestSpider(scrapy.Spider):
name = 'test'
start_urls = ['https://rejestradwokatow.pl/adwokat/list/strona/1/sta/2,3,9']
custom_settings = {
'CONCURRENT_REQUESTS_PER_DOMAIN': 1,
'DOWNLOAD_DELAY': 1,
'USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
def parse(self, response):
wev={}
tic = response.xpath("//div[@class='line_list_K']//div//span//text()").getall()
det = response.xpath("//div[@class='line_list_K']//div//div//text()").getall()
wev[tuple(tic)]=[i.strip() for i in det]
yield wev
I am trying to extract information but they will give me error of unshapable list these is page link https://rejestradwokatow.pl/adwokat/abaewicz-agnieszka-51004
import scrapy
from scrapy.http import Request
from scrapy.crawler import CrawlerProcess
class TestSpider(scrapy.Spider):
name = 'test'
start_urls = ['https://rejestradwokatow.pl/adwokat/list/strona/1/sta/2,3,9']
custom_settings = {
'CONCURRENT_REQUESTS_PER_DOMAIN': 1,
'DOWNLOAD_DELAY': 1,
'USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
def parse(self, response):
wev={}
tic = response.xpath("//div[@class='line_list_K']//div//span//text()").getall()
det = response.xpath("//div[@class='line_list_K']//div//div//text()").getall()
wev[tuple(tic)]=[i.strip() for i in det]
yield wev
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字典键不能变形,并且必须是可见的。尝试以下操作:
甚至更简单:
Dictionary keys cannot be mutable and must be hashable. Try this:
or even simpler:
您必须使用
zip()
从tic
和det
中使用 将给出
csv
具有正确的值编辑:
最终您应该首先获得行,然后搜索
name
and code> value 在每个中排。而且,如果某些行没有某些价值,则有时可以更安全。或当行具有Unusulal的值时,例如
电子邮件
,该值由JavaScript添加(但是scrapy
可以运行JavaScript),但它将其作为属性将其作为tag< div class = class = “ address_e” data-ea =“ adwokat.adach” data-eb =“ gmail.com”>
,因为只有某些页面具有
email> email
,因此它可能不会在文件中添加此值 -因此,它需要将默认值添加到wev = {'email:':':'',...}
开始时。其他值也可能存在同样的问题。完整的工作代码
You have to use
zip()
to group values fromtic
anddet
and this will give
wev
withand this will give
CSV
with correct valuesEDIT:
Eventually you should first get rows and later search
name
andvalue
in every row.And this method sometimes can be safer if some row don't has some value. Or when row has unusuall value like
email
which is added by JavaScript (butscrapy
can run JavaScript) but it keep it as attributes in tag<div class="address_e" data-ea="adwokat.adach" data-eb="gmail.com">
Because only some pages have
Email
so it may not add this value in file - so it need to add default value towev = {'Email:': '', ...}
at start. The same problem can be with other values.Full working code
检查
TIC
的数据类型。它很可能是不能是字典键的列表。也许您可以根据您的要求将其铸造成元组。Check the datatype of
tic
. It is most probably a list that cannot be dictionary keys. Maybe you can cast it to a tuple based on your requirements.