【发布时间】:2019-12-03 00:44:42
【问题描述】:
我正在用 scrapy 和 python 创建一个网络爬虫。我正在抓取的页面将每个项目都构造为卡片,我可以从这些卡片中抓取一些信息(名称、位置),但我也想通过点击卡片 > 新页面 > 点击来获取信息打开表单的新页面上的按钮 > 从表单中抓取值。我应该如何构造解析函数,我需要嵌套循环还是单独的函数..?
class StackSpider(Spider):
name = "stack"
allowed_domains = ["example.com"]
start_urls = ["example.com/page"]
def parse(self, response):
for page_url in response.css('a[class ~= search- card]::attr(href)').extract():
page_url = response.urljoin(page_url)
yield scrapy.Request(url=page_url, callback=self.parse)
for vc in response.css('div#vc-profile.container').extract():
item = StackItem()
item['name'] = vc.xpath('//*[@id="vc-profile"]/div/div[2]/div[1]/div[1]/h1/text()').extract()
item['firm'] = vc.expath('//*[@id="vc-profile"]/div/div[2]/div[1]/div[2]/h2/text()[1]').extract()
item['pos'] = vc.expath('//*[@id="vc-profile"]/div/div[2]/div[1]/div[2]/h2/text()[2]').extract()
em = vc.xpath('/*[@id="vc-profile"]/div/div[1]/div[2]/div[2]/div/div[1]/button').extract()
item['email'] = em.xpath('//*[@id="email"]/value').extract()
yield item
scraper 正在爬行,但什么也没输出
【问题讨论】:
标签: python authentication web-scraping pagination scrapy