Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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 Spider不看下一页?_Python_Web Scraping_Scrapy - Fatal编程技术网

Python 为什么Scrapy Spider不看下一页?

Python 为什么Scrapy Spider不看下一页?,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,我的代码在下一页不起作用。它只会刮取第一页,而不会跟随下一页链接。我使用的是一个蟒蛇虚拟环境 # -*- coding: utf-8 -*- import scrapy import logging class Dgoodyman16Spider(scrapy.Spider): name = 'dgoodyman16' allowed_domains = ['www.medicregister.com'] start_urls = ['https://www.medic

我的代码在下一页不起作用。它只会刮取第一页,而不会跟随下一页链接。我使用的是一个蟒蛇虚拟环境

# -*- coding: utf-8 -*-
import scrapy
import logging

class Dgoodyman16Spider(scrapy.Spider):
    name = 'dgoodyman16'
    allowed_domains = ['www.medicregister.com']
    start_urls = ['https://www.medicregister.com/USA/list/suppliers.asp']

    def parse(self, response):
        all_lists = response.xpath('//a[@class="TopicHeaderSupplier"]')
        for lists in all_lists:
            title = lists.xpath('.//text()').get()
            links = lists.xpath('.//@href').get()


            yield response.follow(url=links, callback=self.parse_lists, meta={'lists_title': title})

    def parse_lists(self, response):

        title = response.request.meta['lists_title']

        for data in response.xpath('//div[@class="vcard"]'):
            raw_html = data.xpath('.//div[@style="line-height: 1.5;"]').extract()
            tel = data.xpath('.//span[@class="tel"]/text()').get()
            # email = response.xpath('(//div[@class="vcard"]/a)[2]/@href').get()


        yield {
            'Title': title,
            'html': raw_html,
            'Phone': tel
        }

        next_page = response.xpath('((//div[@class="margin-5"])[2]/a)[10]').get()
        if next_page:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(url=next_page, callback=self.parse)

你是否考虑在导航中收集所有URL,删除重复,并跟随它们?集合中的URL是有效的

relative\u url=set(
xpath('//div[contains(@class,“margin-5”)]/a/@href').getall()
)
绝对URL={
相对url中url的response.urljoin(url)
}


我建议您打开一个scrapy shell,获取start_URL中给出的URL,并重新检查您的唯一xpath。如果它没有返回一个相对URL,你就知道为什么刮板会停在这里

更好的方法是将完整列表放入开始URL:

start_urls = ['https://www.medicregister.com/USA/Manufacturers/Suppliers/Page%d/cid.htm' % i for i in range(1,730)]

这比后面的几页要快得多,这不会异步发生

我使用的XPath表达式是唯一的。所以,我不认为复制会有问题,因为没有重复的链接。这是一个非常糟糕的决定,因为页面的数量可以随着时间的推移而改变。@Michael,你可以得到第一页并进行计算,为了简洁起见,我省略了这一点。