我正在尝试从 http://www.emoryhealthcare.org/locations/offices/advanced-digestive-care-1.html 中抓取信息.
我想抓取出现在页面下三分之一的专业,即“胃肠病学”和“内科医学”。当我检查该元素时,我发现它是 li
的<div class="module bordered specialist">
然而,当我尝试循环遍历汤并打印每个找到的项目时,返回的结果与预期不同。
<div class="module bordered specialist">
<ul>
<li>Cardiac Care</li>
<li>Transplantation</li>
<li>Cancer Care (Oncology)</li>
<li>Diagnostic Radiology</li>
<li>Neurosciences</li>
<li>Mental Health Services</li>
</ul>
</div>
当我在浏览器中打开网站时,我看到上述值在内容切换到预期结果之前闪烁。有没有办法提高我能够抓取我想要抓取的项目的可能性?
最佳答案
只需使用 selenium 等待几秒钟,然后像以前一样进行解析。这似乎成功了。
from selenium import webdriver
import os
import time
from bs4 import BeautifulSoup
chromedriver = "/Users/Rafael/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get('http://www.emoryhealthcare.org/locations/offices/advanced-digestive-care-1.html')
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html, 'lxml')
results = soup.find_all("div", { "class" : "module bordered specialist" })
print(results[0].text) #prints GastroenterologyInternal Medicine
关于python - BeautifulSoup 不会提取所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40180259/