刮痧爬只有第1页

2024-04-25 14:07:50 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我正在用scrapy做一个项目,我需要从业务目录http://directory.thesun.co.uk/find/uk/computer-repair
我面临的问题是:当我试图抓取网页时,我的爬虫只获取第一页的详细信息,而我还需要获取其余9页的详细信息;即全部10页。。 我在下面显示我的蜘蛛代码和项目.py和设置.py 请查看我的代码并帮助我解决它

蜘蛛代码:

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

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

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//div[@class="abTbl "]')
        items = []
        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)
        return items

我的项目.py代码如下:

from scrapy.item import Item, Field

class Project2Item(Item):
    Catogory = Field()
    Bussiness_name = Field()
    Description = Field()
    Number = Field()
    Web_url = Field()
    adress_name = Field()
    Photo_name = Field()
    Photo_path = Field()

我的设置.py是:::

BOT_NAME = 'project2'

SPIDER_MODULES = ['project2.spiders']
NEWSPIDER_MODULE = 'project2.spiders'

请帮忙 我也需要从其他页面中提取细节。。。你知道吗


Tags: 代码namefrompyimportfieldsiteextract