好的,所以我想通了。我使用了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}
}
不幸的是,'“引用”'弹出窗口是在底层的网页从'Cite得到''一个事件javascript'。由于Beautifulsoup是一个解析器而不是交互式网页浏览客户端,因此您可能需要考虑使用'selenium','PhantomJS'或其他工具解决此问题。 – davedwards
我试着用'selenium'来解决它,但是当我尝试抓取几个物品时google会被吓到 –
@downshift你应该添加你的评论作为答案 – ands