Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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 痒痒的蜘蛛只会爬行,不会刮_Python_Web Scraping_Scrapy - Fatal编程技术网

Python 痒痒的蜘蛛只会爬行,不会刮

Python 痒痒的蜘蛛只会爬行,不会刮,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,我正在做一个项目,在这个项目中,我使用scrapy从web站点中刮取项目,但问题是,该站点前两页的XPath与其他页面的XPath不同。 结果,我的爬行器只是从前两个页面中刮取项目,然后简单地在其他页面上爬行。 我怎样才能使我的蜘蛛也刮页面的项目呢 我还包括我的蜘蛛在这里,这样你可以看到我的蜘蛛,如果需要的话 from scrapy.spider import BaseSpider from scrapy.selector import HtmlXPathSelector from projec

我正在做一个项目,在这个项目中,我使用scrapy从web站点中刮取项目,但问题是,该站点前两页的XPath与其他页面的XPath不同。 结果,我的爬行器只是从前两个页面中刮取项目,然后简单地在其他页面上爬行。 我怎样才能使我的蜘蛛也刮页面的项目呢

我还包括我的蜘蛛在这里,这样你可以看到我的蜘蛛,如果需要的话

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from project2.items import Project2Item
from scrapy.http import Request

class ProjectSpider(BaseSpider):
    name = "project2spider"
    allowed_domains = ["http://directory.thesun.co.uk/"]
    current_page_no = 1
    start_urls = [
        'http://directory.thesun.co.uk/find/uk/computer-repair'
        ]

    def get_next_url(self, fired_url):
        if '/page/' in fired_url:
            url, page_no = fired_url.rsplit('/page/', 1)
        else:
            if self.current_page_no != 1:
                #end of scroll
                return 
        self.current_page_no += 1
        return "http://directory.thesun.co.uk/find/uk/computer-repair/page/%s" % self.current_page_no

# the parse procedure, and here is the codes which declares which field to scrape. 
    def parse(self, response):
        fired_url = response.url
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//div[@class="abTbl "]')
   
        for site in sites:
            item = Project2Item()
            item['Catogory'] = site.select('span[@class="icListBusType"]/text()').extract()
            item['Bussiness_name'] = site.select('a/@title').extract()
            item['Description'] = site.select('span[last()]/text()').extract()
            item['Number'] = site.select('span[@class="searchInfoLabel"]/span/@id').extract()
            item['Web_url'] = site.select('span[@class="searchInfoLabel"]/a/@href').extract()
            item['adress_name'] = site.select('span[@class="searchInfoLabel"]/span/text()').extract()
            item['Photo_name'] = site.select('img/@alt').extract()
            item['Photo_path'] = site.select('img/@src').extract()
            #items.append(item)
            yield item
        next_url = self.get_next_url(fired_url)
        if next_url:
            yield Request(next_url, self.parse, dont_filter=True)
        
对于其他页面,我需要使用以下内容:
sites=hxs.select('//div[@class=“icListItem”]')

如何将其包含在我的spider中,以便它也可以从其他页面中刮取项目

目前,它只需抓取前两页,然后简单地在其他页面上爬行。

到目前为止,您尝试了什么? 一种解决方案是在调用下一页时使用类似索引的参数作为元数据传递。比如:

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    2nd_xpath = False
    try:
        if response.meta['index'] > 1:
            2nd_xpath = True
        index = response.meta['index']
    except KeyError:
        index = 0
    sites = (hxs.select('//div[@class="icListItem"]') if 2nd_xpath
             else hxs.select('//div[@class="abTbl "]'))

    ...

    request = Request(next_url, self.parse, dont_filter=True)
    request.meta['index'] = index + 1
    yield request

这段代码当然可以改进,但你明白了。

奇怪的是,在9个小时内,没有人给出任何解决这个问题的建议。。有人很可能没有你写的那种感觉。。。i、 e:“我如何让我不懂的这段代码做这个非常简单的额外的事情?”(根本不学习编程)。啊。与问题本身完全无关,但自从我第一次看到这段代码以来,这一直困扰着我:“类别”不是“目录”、“业务名称”不是“业务名称”和“地址名称”不是“地址名称”。呼吸。仅供参考,这是刮(刮,刮,刮)而不是刮