私はそれを理解しました。私はあなたがリンクをクリックし、結果のHTMLの出力を得るようなアクションを実行できるようにするならば、仮想ブラウザを作成するPythonのためのセレンモジュールを使用しました。この問題を解決するには別の問題がありました。それ以外の場合はページをロードしなければなりませんでした。ポップアップのdivにコンテンツ「Loading ...」が返されたので、pythonの時間モジュールをtime.sleep(2)
に2秒間使用しましたロードするコンテンツ。次に、BeautifulSoupを使用して結果のHTML出力を解析して、クラス「gs_citi」を持つアンカータグを見つけました。その後、アンカーからhrefを引き出し、これを要求のあるPythonモジュールに入れます。最後に、デコードされたレスポンスをローカルファイル - scholar.bibに書きました。
私はここで、これらの命令を使用して私のMac上のchromedriverとセレンをインストール:次に、これらの命令を使用してファイアウォールの問題を停止できるようにするためのpythonファイルが署名し https://gist.github.com/guylaor/3eb9e7ff2ac91b7559625262b8a6dd5f
を: Add Python to OS X Firewall Options?
次のコードIであります出力ファイル "scholar.bib"を生成するために使用されます。
import os
import time
from selenium import webdriver
from bs4 import BeautifulSoup as soup
import requests as req
# Setup Selenium Chrome Web Driver
chromedriver = "/usr/local/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
# Navigate in Chrome to specified page.
driver.get("https://scholar.google.com/scholar?hl=en&q=Sustainability and the measurement of wealth: further reflections")
# Find "Cite" link by looking for anchors that contain "Cite" - second link selected "[1]"
link = driver.find_elements_by_xpath('//a[contains(text(), "' + "Cite" + '")]')[1]
# Click the link
link.click()
print("Waiting for page to load...")
time.sleep(2) # Sleep for 2 seconds
# Get Page source after waiting for 2 seconds of current page in Chrome
source = driver.page_source
# We are done with the driver so quit.
driver.quit()
# Use BeautifulSoup to parse the html source and use "html.parser" as the Parser
soupify = soup(source, 'html.parser')
# Find anchors with the class "gs_citi"
gs_citt = soupify.find('a',{"class":"gs_citi"})
# Get the href attribute of the first anchor found
href = gs_citt['href']
print("Fetching: ", href)
# Instantiate a new requests session
session = req.Session()
# Get the response object of href
content = session.get(href)
# Get the content and then decode() it.
bibtex_html = content.content.decode()
# Write the decoded data to a file named scholar.bib
with open("scholar.bib","w") as file:
file.writelines(bibtex_html)
これは、これを解決する方法を探している人に役立ちますでる。
Scholar.bibファイル:
残念ながら
@article{arrow2013sustainability,
title={Sustainability and the measurement of wealth: further reflections},
author={Arrow, Kenneth J and Dasgupta, Partha and Goulder, Lawrence H and Mumford, Kevin J and Oleson, Kirsten},
journal={Environment and Development Economics},
volume={18},
number={4},
pages={504--516},
year={2013},
publisher={Cambridge University Press}
}
、 'ことを「シテ」'ポップアップウィンドウあなたがこすりをしたい基本的なWebページに '' Citeから得 'javascript'イベントです。 BeautifulsoupはインタラクティブなWebブラウジングクライアントではなくパーサーであるため、これを 'senenium'、' PhantomJS'などのツールで解決することを検討する必要があります。 – davedwards
「セレン」で解決しようとしましたが、いくつかの項目を取得しようとするとすごくうんざりします。 –
@downshiftあなたは答えとしてあなたのコメントを追加する必要があります – ands