我最近开始用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}
一种更简单的方法是将
scrapy.spiders.CrawlSpider
类作为子类,并指定rule
属性但是,请确保不要使用
parse
作为回调函数,因为scrapy.spiders.CrawlSpider
使用parse
方法来实现其逻辑。在您的代码中存在类函数
parse_page
的缩进问题,您错误地将其命名为“parse”而不是“parse\u page”。这可能就是你的代码不能正常工作的原因。修改后的代码如下所示,对我来说效果很好:参考https://github.com/NilanshBansal/Craigslist_Scrapy/blob/master/craigslist/spiders/jobs.py
编辑: 注释该行允许_domains也让爬虫在其他域上工作。
回复您在https://stackoverflow.com/a/52792350的代码
相关问题 更多 >
编程相关推荐