2017-08-25 40 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"}) 

gs_citd给我只有这一行<div aria-live="assertive" id="gs_citd"></div>,并在其下方没有达到任何水平。 gs_cit1也返回None

由于出现这一形象

在我要到达突出显示类要能抓住读者预约。

请问您能帮忙吗?

+0

不幸的是,'“引用”'弹出窗口是在底层的网页从'Cite得到''一个事件javascript'。由于Beautifulsoup是一个解析器而不是交互式网页浏览客户端,因此您可能需要考虑使用'selenium','PhantomJS'或其他工具解决此问题。 – davedwards

+0

我试着用'selenium'来解决它,但是当我尝试抓取几个物品时google会被吓到 –

+0

@downshift你应该添加你的评论作为答案 – ands

回答

2

好的,所以我想通了。我使用了python的selenium模块,它创建了一个虚拟浏览器,如果你愿意的话,它可以让你执行一些操作,比如点击链接并获得HTML输出。在解决这个问题时遇到了另一个问题,那就是页面必须被加载,否则它只是在弹出的div中返回内容“Loading ...”,所以我使用了Python时间模块time.sleep(2) 2秒,要加载的内容。然后,我使用BeautifulSoup解析生成的HTML输出,以找到具有类“gs_citi”的锚标记。然后从锚中拉出href,并将其放入带有“requests”python模块的请求中。最后,我将解码的响应写入本地文件 - scholar.bib。 https://gist.github.com/guylaor/3eb9e7ff2ac91b7559625262b8a6dd5f

然后通过Python文件签署允许使用这些指令停止防火墙问题: Add Python to OS X Firewall Options?

以下是我的代码

我在这里使用这些说明安装chromedriver和硒对我的Mac用于生成输出文件“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,这是一个非常彻底的解决方案...我只想澄清几件事情...... 首先,为什么不解决它,直到最后使用硒。我能够模拟所有的点击,直到我用硒取得引文。关于硒的一点是,当我为多篇论文做这件事时,谷歌知道这是一个自动化的过程,并开始要求验证,这就停止了过程。你认为你的解决方案会克服这个问题吗? 另一点是,硒有一个'implicitly_wait()'函数,我们可以用它来代替'time.sleep()'。 –

+0

没有意识到selenium提供'implicity_wait()'函数,我只是认为我们只需要使用bot自动化就能获得正确的源代码,但我相信您可以轻松地使用广泛的硒库。 – kyle

+0

我可以用硒@kyle做,但我的担心是认证问题。另外,例如,将硒方法与一系列论文结合使用,我想这会使记忆和时间效率低下。 –

相关问题