2017-08-25 33 views
0

に到達していない私はBeautifulsoupは、子要素

import requests as req 
from bs4 import BeautifulSoup as soup 

url = r'https://scholar.google.com/scholar?hl=en&q=Sustainability and the measurement of wealth: further reflections' 

session = req.Session() 
content = session.get(url) 
html2bs = soup(content.content, 'lxml') 
gs_cit = html2bs.select('#gs_cit') 
gs_citd = html2bs.find('div', {'id':"gs_citd"}) 
gs_cit1 = html2bs.find('div', {'id':"gs_cit1"}) 

Googleの学者ページをこすりしようとすると、次のコードを書きましたが、gs_citdは私にだけ、この行<div aria-live="assertive" id="gs_citd"></div>を与え、その下の任意のレベルに到達していません。またgs_cit1Noneを返します。

はこのイメージ

に登場するとして、私はBibTeX文献の引用をつかむことができるように強調表示されたクラスに到達したいです。

お願いします。

+0

、 'ことを「シテ」'ポップアップウィンドウあなたがこすりをしたい基本的なWebページに '' Citeから得 'javascript'イベントです。 BeautifulsoupはインタラクティブなWebブラウジングクライアントではなくパーサーであるため、これを 'senenium'、' PhantomJS'などのツールで解決することを検討する必要があります。 – davedwards

+0

「セレン」で解決しようとしましたが、いくつかの項目を取得しようとするとすごくうんざりします。 –

+0

@downshiftあなたは答えとしてあなたのコメントを追加する必要があります – ands

答えて

2

私はそれを理解しました。私はあなたがリンクをクリックし、結果の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} 
} 
+0

ありがとう、@Kyle、これは非常に徹底的な解決策です...私はちょっとしたことを明確にしたいと思っています.... まず、セレンを使って最後まで解決してみませんか?私はセレンを使用して引用をつかむまで、すべてのクリックをシミュレートすることができました。セレンのポイントは、私がいくつかの論文についてそれを行うとき、Googleはそれが自動化されたプロセスであることを知っており、プロセスのプロセスを停止する検証を求め始めるということです。あなたのソリューションはこの問題を克服すると思いますか? もう一つのポイントは、seleniumは 'time.sleep()'の代わりに 'implicitly_wait()'関数を使用できることです。 –

+0

'implicity_wait()'関数がセレンから入手可能であることを認識していませんでした。私はちょうど、正しいソースを取得するために必要なものをボットで自動化する必要があると思っていましたが、広範なセレンライブラリ。 – kyle

+0

私はセレニウム@kyleでそれを行うことができますが、私の心配は認証の問題です。また、紙のリストでセレンメソッドを使用すると、メモリと時間が非効率になります。 –