簡體   English   中英

Web 用 Python 抓取 Google Scholar

[英]Web scraping Google Scholar with Python

嗨,我需要使用 bs4 或 Selenium 從 Google Scholar 的記錄中提取摘要和 DOI。 我對這樣的“academic.oup”頁面有疑問:https://academic.oup.com/eurheartj/article-abstract/42/Supplement_1/ehab724.1650/6394300

我無法使用 bs4 或 selenium 提取 DOI 或摘要。 這是我的 selenium 代碼:

driver = webdriver.Chrome('/Users/cante/Downloads/chromedriver_win32/chromedriver.exe')
driver.get('https://academic.oup.com/eurheartj/article-abstract/42/Supplement_1/ehab724.1650/6394300')
abstract = driver.find_elements_by_xpath('//*[@id="ContentTab"]/div[1]/div/div/section')
doi_element = driver.find_elements_by_xpath('//*[@id="ContentColumn"]/div[3]/div[1]/div/div/div[3]/div[1]/div/a')

for element in abstract:
    print(x, element.text)
for element in doi_element:
    print(x, element.text)

driver.quit()

我的結果是空的(我從頁面的鏈接中找到了 XPath)。 這是我的 bs4 代碼:

response = requests.get('https://academic.oup.com/eurheartj/article-abstract/42/Supplement_1/ehab724.1650/6394300', headers=headers)
if response.status_code != 200:
    print('Status code:', response.status_code)
    raise Exception('Failed to fetch web page ')

page = BeautifulSoup(response.text, 'html.parser')

for entry in page.find_all("seciton", attrs={"class": "abstract"}):
    print(entry.get_text())

而且我的結果也是空的。 問題是什么?

我建議使用beautifulsoup而不是selenium

正如 AlexDotis 在評論中提到的,在 GET 請求中使用headers頭來獲得成功的響應,即200 Response 沒有它,您將獲得404 Response

此代碼將打印Abstract 我不確定你所說的DOI是什么意思,所以我把它留給你了

import requests
from bs4 import BeautifulSoup

url = 'https://academic.oup.com/eurheartj/article/42/Supplement_1/ehab724.1650/6394300'
headers = {"User-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')

s = soup.find('section', class_='abstract')

divs = s.find_all('div', class_='sec')
for div in d:
    title = div.find('div', class_='title').text.strip()
    tex = None
    p_tex = div.find('p')
    if p_tex:
        tex = p_tex.text.strip()
    print(f'\n{title}\n{tex}\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM