Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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 无法在Xpath中选择特定元素_Python_Xpath_Scrapy - Fatal编程技术网

Python 无法在Xpath中选择特定元素

Python 无法在Xpath中选择特定元素,python,xpath,scrapy,Python,Xpath,Scrapy,我正在尝试删除此页面: 我几乎成功地抓取了每个类别部分中的所有链接。但出于某种原因,所有这些。。链接没有通过,即使它们的Xpath应该与其他链接相同 我目前正在做的是查找属于带有类值类别导航链接的标记的所有href值 我在Python上使用Scrapy,因此我从脚本中获取信息的方式是: response.xpath("//a[@class='category-navigation--link']/@href") 这很好地提供了页面中的大部分链接,除了这些。。链接,但我不明白为什么。它们看起来和

我正在尝试删除此页面:

我几乎成功地抓取了每个类别部分中的所有链接。但出于某种原因,所有这些。。链接没有通过,即使它们的Xpath应该与其他链接相同

我目前正在做的是查找属于带有类值类别导航链接的标记的所有href值

我在Python上使用Scrapy,因此我从脚本中获取信息的方式是:

response.xpath("//a[@class='category-navigation--link']/@href")
这很好地提供了页面中的大部分链接,除了这些。。链接,但我不明白为什么。它们看起来和其他的一样,但是xpath选择器不知何故无法获取信息

编辑:这是我的代码。它应该是一样的美丽汤的例子PS1212张贴在这里,唯一的区别是,我返回链接。它将错过href字段中所有这些URL。。由于某种原因

import scrapy
from ..items import CoolBlueItems


class QuoteSpider(scrapy.Spider):
    name = "coolblue2"

    start_urls = ["https://www.coolblue.nl/en/our-assortments]

    def __init__(self):

        self.declare_xpath()

    def declare_xpath(self):

        self.getAllSubCategoriesUrlsXpath = "//a[@class='category-navigation--link']/@href"

    def parse(self, response):

        item = CoolBlueItems()

        urls_list = []

        no_scrap_urls = ["/en/promotion", "/en/second-chance", "/en/gift-cards", "/en/coolblue-fan-products", "/en/all-brands"]

        for Urls in response.xpath(self.getAllSubCategoriesUrlsXpath).getall():

            current_url = Urls.strip()

            if current_url not in urls_list and current_url not in no_scrap_urls and current_url.count("/") == 2:
                urls_list.append(current_url)
                item["Url"] = response.urljoin(current_url)
                yield item
我遵循PS1212的建议。必须进行一些修改,因为它向re抛出了一个与信息处理方式相关的错误。功能:

import scrapy
from ..items import CoolBlueItems


    class QuoteSpider(scrapy.Spider):
        name = "coolblue2"

        start_urls = ["https://www.coolblue.nl/en/our-assortments]

        for a in response.css("a.category-navigation--link::attr('href')").getall():
            item["Url"] = re.split('/', a)
            yield item
它仍然跳过了我想要的元素。以下是输出的第一个条目:

Category,CurrentPrice,OriginalPrice,Title,Url
,,,,",en,laptops"
,,,,",en,laptops,apple-macbook"
,,,,",en,desktops"
,,,,",en,monitors"
,,,,",en,keyboards"
编辑:问题在于选择器本身。我可以让我的脚本工作,但我仍然很好奇为什么CSS选择器工作而xpath不工作。下面是我所做的一个测试,我使用xpath和css使用某个类删除a部分中的所有元素:

>>> response.xpath("//a[@class='category-navigation--link']")[4].getall()
['<a class="category-navigation--link" href="/en/keyboards" rel="nofollow">\n                    Keyboards\n                </a>']
>>>



>>> response.css('a.category-navigation--link')[4].get()
'<a class="category-navigation--link category-navigation--link--black" href="/en/laptops-desktops-monitors" data-trackclickevent="Homepage categor
y navigation|Computers &amp; tablets|More..">\n                                                                        More..\n
                                                 </a>'
如您所见,数组的第5个元素(在这两种情况下都是索引4)返回不同的值。我一定是在Xpath选择器中的某个地方出错了。

请尝试以下操作:

import requests, re
from bs4 import Beatifulsoup

htl = requests.get('https://www.coolblue.nl/en/our-assortment')
soup = Beautifulsoup(htl.text, 'lxml')

a_tages = soup.findAll('a', class_='category-navigation--link')
for a in a_tages:
       href_list = re.split('/',a.get('href')))
       print(href_list.pop())
通过Scrapy:

import scrapy
from ..items import CoolBlueItems

class QuoteSpider(scrapy.Spider):
      name = "coolblue2"
      start_urls = ["https://www.coolblue.nl/en/our-assortments]

      def parse(self, response):
          item = CoolBlueItems()

          for a in response.css('a.category-navigation--link'):
              url = a.css("::attr('href')").extract()
              split_url = re.split('/', url))
              print(split_url.pop())
输出:


向我们展示您正在使用的代码以及您得到的结果。说它以某种方式无法获得信息并不能告诉我们太多。PS1212答案是有效的,最后一行笔记本电脑桌面显示器是我所缺少的,但我需要通过scrapy来完成。太好了!它也适用于scrapy。你只需要根据这一点更改选择器模式。在PS1212帖子中添加了我的代码。我认为它应该像你的一样工作,它只是返回URL而已。@JumBê刚刚更新了我对零碎代码的回答。希望它能解决您的查询。发现了它的实现中的一些问题,但我设法使它工作。它仍然返回与我的xpath示例相同的结果。是因为我做了修改吗?我用我所做的更新了帖子
laptops
apple-macbook
desktops
monitors
laptops-desktops-monitors
...