私はGoogle Scholar search resultsをクロールしようとしており、検索結果に一致する各結果のすべてのBiBTeX形式を取得しようとしています。今私はスプラッシュを持つScrapyクローラーを持っています。私は、引用のBibTeX形式のhref
を得る前に、 "引用"リンクをクリックしてモーダルウィンドウをロードするluaスクリプトを持っています。しかし、複数の検索結果があり、複数の「引用」リンクがあることを確認するには、それらをすべてクリックして個々のBibTeXページをロードする必要があります。ここで スプラッシュluaスクリプトを複数回クリックして訪問する
import scrapy
from scrapy_splash import SplashRequest
class CiteSpider(scrapy.Spider):
name = "cite"
allowed_domains = ["scholar.google.com", "scholar.google.ae"]
start_urls = [
'https://scholar.google.ae/scholar?q="thermodynamics"&hl=en'
]
script = """
function main(splash)
local url = splash.args.url
assert(splash:go(url))
assert(splash:wait(0.5))
splash:runjs('document.querySelectorAll("a.gs_nph[aria-controls=gs_cit]")[0].click()')
splash:wait(3)
local href = splash:evaljs('document.querySelectorAll(".gs_citi")[0].href')
assert(splash:go(href))
return {
html = splash:html(),
png = splash:png(),
href=href,
}
end
"""
def parse(self, response):
yield SplashRequest(self.start_urls[0], self.parse_bib,
endpoint="execute",
args={"lua_source": self.script})
def parse_bib(self, response):
filename = response.url.split("/")[-2] + '.html'
with open(filename, 'wb') as f:
f.write(response.css("body > pre::text").extract()[0])
私はquerySelectorAll
呼び出しを実行するとき、私はLuaのスクリプトに「シテ」リンクのインデックスを渡す必要があると思っていますが、私は見つけることができないよう他の変数を関数に渡すことができます。また、私はBibTeXを取得した後に元の結果ページに戻るためにいくつかの汚いjavascript history.back()
を行う必要があると仮定しますが、私はこれを扱うよりエレガントな方法があると感じています。
FYI: 'SplashRequest(...、args = {" lua_source ":スクリプト、" n ":n})'に 'n '引数を渡してスクリプトから' splash.args n。この方法では、文字列の書式設定は必要ありません。文字列の書式設定にはいくつかの欠点があります。コードの数が増え、有効なLua(整数変数ではn/a)にエスケープする必要があり、キャッシングでうまくいきません(スプラッシュでスクリプトをキャッシュできるため、リクエストごとにスクリプトを送信する)。 –