Scrapy - xpath查询返回< li>list,但仅第一个元素重复

yftpprvb  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(175)

我尝试使用以下命令获取ul标记中的所有li/a元素:

  1. def parse(self, response):
  2. newItem = Item()
  3. list = response.xpath('//ul[@id="ad-list"]/li//a[@data-ds-component="DS-Link"]')

字符串
当我循环遍历列表时,只有我抓取的每个页面的第一个元素在列表中重复:

  1. for item in list:
  2. print(type(offer))
  3. newItem["product_id"] = item.xpath('//@data-lurker_list_id').get()
  4. newItem["url"] = item.xpath('//@href').get()
  5. newItem["title"] = item.xpath('//@title').get()


我已经检查过了,我在ul中得到了正确的li数作为列表变量中的响应,但是只出现了第一个li项。
为了在响应后验证它,我尝试打印以下内容:

  1. print("INFO:")
  2. print(len(offerList))
  3. print(list(map(lambda x: x.xpath('//@data-lurker_list_id').get(), offerList)))


我得到这个输出:

  1. INFO:
  2. 50
  3. ['1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301', '1204352301']


你知道我错过了什么吗?

qf9go6mv

qf9go6mv1#

我刚发现问题。基本上应该只得到ul,得到我想要的li列表。

  1. list = response.xpath('//ul[@id="ad-list"]')

字符串

相关问题