我正在嘗試從 Google Scholar 中抓取一些資料scrapy,我的代碼如下:
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蜘蛛結合起來,它運行良好并且速度非常快。
uj5u.com熱心網友回復:
查看以下實作。這應該會為您提供該頁面耗盡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()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425260.html
上一篇:AndroidStudio:在視圖類上定義的android:onClick屬性的父或祖先背景關系中找不到方法XXXXX(View)
