用 Scrapy 点击 Google 学者按钮

Clicking Google Scholar Button with Scrapy

我正在尝试使用 scrapy 从 Google Scholar 抓取一些数据,我的代码如下:

import scrapy
class TryscraperSpider(scrapy.Spider):
    name = 'tryscraper'
    start_urls = ['https://scholar.google.com/citations?hl=en&user=JUn8PgwAAAAJ&pagesize=100&view_op=list_works&sortby=pubdate']

    def parse(self, response):
        for link in response.css('a.gsc_a_at::attr(href)'):
            yield response.follow(link.get(), callback=self.parse_scholar)
       
       
    def parse_scholar(self, response):
        try: 
            yield {
               'authors': response.css('div.gsc_oci_value::text').get().strip(),
               'journal': response.css('div.gsc_oci_value::text').extract()[2].strip(),
               'date': response.css('div.gsc_oci_value::text').extract()[1].strip(),
               'abstract': response.css('div.gsh_csp::text').get()
                 }
        except: 
            yield {
                'authors': response.css('div.gsc_oci_value::text').get().strip(),
                'journal': response.css('div.gsc_oci_value::text').extract()[2].strip(),
                'date': response.css('div.gsc_oci_value::text').extract()[1].strip(),
                'abstract': 'NA'
                 }
            

此代码运行良好,但它只提供作者的前 100 篇论文,我想将它们全部抓取,但我需要对蜘蛛进行编码才能同时按下“显示更多”按钮。我在相关帖子中看到 scrapy 没有内置函数来执行此操作,但也许您可以合并 selenium 中的功能来完成这项工作。不幸的是,我有点新手,因此完全迷路了,有什么建议吗?提前致谢。

selenium 代码可以完成这项工作,但我希望它能与我的 scrapy 蜘蛛结合使用,它运行良好且速度非常快。

检查以下实施。这应该会为您提供该页面耗尽 show more 按钮的所有结果。

import scrapy
import urllib
from scrapy import Selector

class ScholarSpider(scrapy.Spider):
    name = 'scholar'
    start_url = 'https://scholar.google.com/citations?'
    
    params = {
        'hl': 'en',
        'user': 'JUn8PgwAAAAJ',
        'view_op': 'list_works',
        'sortby': 'pubdate',
        'cstart': 0,
        'pagesize': '100'
    }

    def start_requests(self):
        req_url = f"{self.start_url}{urllib.parse.urlencode(self.params)}"
        yield scrapy.FormRequest(req_url,formdata={'json':'1'},callback=self.parse)


    def parse(self, response):
        if not response.json()['B']:
            return

        resp = Selector(text=response.json()['B'])
        for item in resp.css("tr > td > a[href^='/citations']::attr(href)").getall():
            inner_link = f"https://scholar.google.com{item}"
            yield scrapy.Request(inner_link,callback=self.parse_content)

        self.params['cstart']+=100
        req_url = f"{self.start_url}{urllib.parse.urlencode(self.params)}"
        yield scrapy.FormRequest(req_url,formdata={'json':'1'},callback=self.parse)


    def parse_content(self,response):
        yield {
            'authors': response.css(".gsc_oci_field:contains('Author') + .gsc_oci_value::text").get(),
            'journal': response.css(".gsc_oci_field:contains('Journal') + .gsc_oci_value::text").get(),
            'date': response.css(".gsc_oci_field:contains('Publication date') + .gsc_oci_value::text").get(),
            'abstract': response.css("#gsc_oci_descr .gsh_csp::text").get()
        }