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 scrapy xpath可以';我得不到价值_Python_Xpath_Scrapy_Scrapy Spider - Fatal编程技术网

Python scrapy xpath可以';我得不到价值

Python scrapy xpath可以';我得不到价值,python,xpath,scrapy,scrapy-spider,Python,Xpath,Scrapy,Scrapy Spider,我有一个网站,我想保存两个跨度元素的价值 这是我的html代码的相关部分: 我创建了一个蜘蛛: from scrapy.spiders import Spider from scrapy.selector import Selector class MySpdier(Spider): name = "list" allowed_domains = ["example.com"] start_urls = [ "https://www.example

我有一个网站,我想保存两个跨度元素的价值

这是我的html代码的相关部分:


我创建了一个蜘蛛:

from scrapy.spiders import Spider
from scrapy.selector import Selector

class MySpdier(Spider):

    name = "list"
    allowed_domains = ["example.com"]
    start_urls = [
        "https://www.example.com"]

    def parse(self, response):
        sel = Selector(response)
        divs = sel.xpath("//div[@class='box-search-product-filter-row']")


        for div in divs:
            sth = div.xpath("/span[class='result']/text()").extract()

            print sth
当我抓取蜘蛛时,它只打印以下内容:

[]


有谁能帮助我如何从我的两个(类编号和类结果)span元素中获取值吗?

您忘记了xpath中的
@
“/span[class='result']/text()”。此外,您要查找的跨度不是一级子级,因此您需要使用
/
而不是
/
。见: 资料来源:


如果您只想选择文本,但示例中的节点没有文本,因此无法在此处使用,那么完整且正确的xpath将是:
“//span[@class='result']]”您忘记了xpath
中的
@
。此外,您要查找的跨度不是一级子级,因此您需要使用
/
而不是
/
。见: 资料来源:


如果您只想选择文本,但示例中的节点没有文本,因此无法在此处使用,那么完整且正确的xpath将是:
“//span[@class='result']”
+'/text()。

这对您很有用

编辑:

from scrapy.spiders import Spider
from scrapy.selector import Selector

class MySpdier(Spider):

    name = "list"
    allowed_domains = ["example.com"]
    start_urls = [
        "https://www.example.com"]

    def parse(self, response):
        sel = Selector(response)
        divs = sel.xpath("//div[@class='box-search-product-filter-row']")    

        for div in divs:
            sth = div.xpath(".//span[@class='result']/text()").extract()    
            print sth

这对你有用

编辑:

from scrapy.spiders import Spider
from scrapy.selector import Selector

class MySpdier(Spider):

    name = "list"
    allowed_domains = ["example.com"]
    start_urls = [
        "https://www.example.com"]

    def parse(self, response):
        sel = Selector(response)
        divs = sel.xpath("//div[@class='box-search-product-filter-row']")    

        for div in divs:
            sth = div.xpath(".//span[@class='result']/text()").extract()    
            print sth

sth=div.xpath(“.//span[@class='result']/text()”).extract()它的工作方式:)sth=div.xpath(“.//span[@class='result']/text()”).extract()它的工作方式:)sth=div.xpath(“.//span[@class='result']/text()”).extract()它的工作方式:)非常感谢。但正如你提到的,我的跨度是空的。。其中有动态生成的文本..:(这可能得到吗?这就是它现在打印的内容:[u']取决于您所说的“动态生成”的含义),如果它在html正文中,那么您的请求就可以确定,如果它是由javascript生成的,那么您需要弄清楚javascript是如何做到这一点的,并在您的爬行器中重新创建它。如果您可以提供您正在爬行的网站,这将非常有用。我不能向您显示该网站,因为它是一个Web和intranet页面。如果我检查那里的Renderer页面的源代码是一个javascript变量:var count=85;我认为[u'']我的span使用这个变量..所以我肯定认为我必须得到这个js变量值..可能吗?:)当然,尝试
import re;count=re.findall('var count.+?(\d+),response.body)[0]
。这就是所谓的正则表达式,它本身就是一个巨大的主题,所以它有点超出了这个问题的范围。相关python文档页面:sth=div.xpath(“.//span[@class='result']/text()”).extract()它的工作方式是:)非常感谢。但正如你提到的,我的跨度是空的。。其中有动态生成的文本..:(这可能得到吗?这就是它现在打印的内容:[u']取决于您所说的“动态生成”的含义),如果它在html正文中,那么您的请求就可以确定,如果它是由javascript生成的,那么您需要弄清楚javascript是如何做到这一点的,并在您的爬行器中重新创建它。如果您可以提供您正在爬行的网站,这将非常有用。我不能向您显示该网站,因为它是一个Web和intranet页面。如果我检查那里的Renderer页面的源代码是一个javascript变量:var count=85;我认为[u'']我的span使用这个变量..所以我肯定认为我必须得到这个js变量值..可能吗?:)当然,尝试
import re;count=re.findall('var count.+?(\d+),response.body)[0]
。这就是所谓的正则表达式,它本身就是一个巨大的主题,所以它有点超出了这个问题的范围。相关python文档页面: