我要抓取的网站是ScienceDirect。单击“显示更多”按钮后,该联盟将可用。我可以单击它,但是单击“显示更多”按钮后无法抓取已加载的从属关系这是代码。 for循环未打印包含从属关系的dl-tag
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium import webdriver
from bs4 import BeautifulSoup
driver = webdriver.Firefox()
driver.get('https://www.sciencedirect.com/science/article/pii/S1571065308000656')
soup = BeautifulSoup(driver.page_source,'html.parser')
time.sleep(7)
try:
element = driver.find_element_by_css_selector('.show-hide-details.u-font-sans')
element.click()
time.sleep(15)
for data in soup.find(id='author-group'):
print(data)
print('---')
except NoSuchElementException:
pass
最佳答案
我认为您需要在单击“显示更多”按钮后将汤的实例化向下移动。
如果我运行以下代码:
driver = webdriver.Firefox()
driver.get('https://www.sciencedirect.com/science/article/pii/S1571065308000656')
time.sleep(3)
try:
element = driver.find_element_by_css_selector('.show-hide-details.u-font-sans')
element.click()
time.sleep(9)
soup = BeautifulSoup(driver.page_source,'html.parser')
for data in soup.find(id='author-group'):
print(data)
print('---')
except NoSuchElementException:
pass
我的输出是:
<span class="sr-only">Author links open overlay panel</span>
---
<a class="author size-m workspace-trigger" href="#!" name="baep-author-id6"><span class="content"><span class="text given-name">Ignaz</span><span class="text surname">Rutter</span><span class="author-ref" id="bfn001"><sup>1</sup></span><svg class="icon icon-envelope" focusable="false" height="24" viewbox="0 0 102 128" width="19.125"><path d="m55.8 57.2c-1.78 1.31-5.14 1.31-6.9 0l-31.32-23.2h69.54l-31.32 23.19zm-55.8-24.78l42.94 32.62c2.64 1.95 6.02 2.93 9.4 2.93s6.78-0.98 9.42-2.93l40.24-30.7v-10.34h-102zm92 56.48l-18.06-22.74-8.04 5.95 17.38 21.89h-64.54l18.38-23.12-8.04-5.96-19.08 24.02v-37.58l-1e1 -8.46v61.1h102v-59.18l-1e1 8.46v35.62"></path></svg></span></a>
---
<dl class="affiliation"><dd>Fakultät für Informatik, Universität Karlsruhe, Germany</dd></dl>
---