如何使用Scrapy抓取新链接

Ste*_*fan 1 python scrapy web-scraping

我最近开始使用Scrapy,所以我不太熟练,所以这确实是一个新手问题。

我在练习中使用一些随机惯例,我在抓取名称和展位号,但是我也想要来自公司的链接,这些链接位于新窗口内,我已经找到并存储了来自锚标签的链接,但是我没有知道如何抓取那些新的链接,任何帮助或指导都将很可爱

import scrapy

class ConventionSpider(scrapy.Spider):
    name = 'convention'
    allowed_domains = ['events.jspargo.com/ASCB18/Public/Exhibitors.aspx?sortMenu=102003']
    start_urls = ['https://events.jspargo.com/ASCB18/Public/Exhibitors.aspx?sortMenu=102003']

    def parse(self, response):
        name = response.xpath('//*[@class="companyName"]')
        number = response.xpath('//*[@class="boothLabel"]')
        link = response.xpath('//*[@class="companyName"]')
        for row, row1, row2 in zip(name, number, link):
            company = row.xpath('.//*[@class="exhibitorName"]/text()').extract_first()
            booth_num = row1.xpath('.//*[@class="boothLabel aa-mapIt"]/text()').extract_first()
            url = row2.xpath('.//a/@href').extract_first()

            yield {'Company': company,'Booth Number': booth_num}
Run Code Online (Sandbox Code Playgroud)

nil*_*sal 5

参见参考https://github.com/NilanshBansal/Craigslist_Scrapy/blob/master/craigslist/spiders/jobs.py

import scrapy
from scrapy import Request

class ConventionSpider(scrapy.Spider):
name = 'convention'
# allowed_domains = ['events.jspargo.com/ASCB18/Public/Exhibitors.aspx?sortMenu=102003']
start_urls = ['https://events.jspargo.com/ASCB18/Public/Exhibitors.aspx?sortMenu=102003']

def parse(self, response):
    name = response.xpath('//*[@class="companyName"]')
    number = response.xpath('//*[@class="boothLabel"]')
    link = response.xpath('//*[@class="companyName"]')
    for row, row1, row2 in zip(name, number, link):
        company = row.xpath('.//*[@class="exhibitorName"]/text()').extract_first()
        booth_num = row1.xpath('.//*[@class="boothLabel aa-mapIt"]/text()').extract_first()
        url = row2.xpath('.//a/@href').extract_first()

        yield Request(url,callback=self.parse_page,meta={'Url':url,'Company': company,'Booth_Number': booth_num)

def parse_page(self,response):
    company = response.meta.get('Company')
    booth_num = response.meta.get('Booth Number')
    website = response.xpath('//a[@class="aa-BoothContactUrl"]/text()').extract_first()

    yield {'Company': company,'Booth Number': booth_num, 'Website': website}
Run Code Online (Sandbox Code Playgroud)

编辑: 注释行allow_domains以使搜寻器也可以在其他域上工作。

/sf/answers/3695464531/回复您的代码