This是我想使用BeautifulSoup從以下網站(https://wwwn.cdc.gov/nchs/nhanes/search/datapage.aspx?Component=Examination)解析的源代碼的圖像。我希望提取span class ='print-only'>屬性中的屬性:htm鏈接。使用BeautifulSoup無法從span元素收集屬性
我的Python代碼看起來像這樣:
import urllib.request
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
url = "https://wwwn.cdc.gov/nchs/nhanes/search/datapage.aspx?Component=Examination"
with urllib.request.urlopen(url) as page:
html_source = page.read()
soup = BeautifulSoup(html_source, 'html5lib')
link = soup.findAll("span", {"class":"print-only"})
打印 '鏈接' 返回一個空列表。我知道在html代碼中有span元素,因爲soup.findAll(「span」)會返回html代碼(儘管沒有在這些span元素的內容中看到名爲「僅打印」的類)。
我注意到在Firefox開發人員窗口中span屬性是灰色的。快速谷歌搜索顯示,這意味着該屬性是隱藏的。這是否意味着它不能用我使用的方法獲得?
這工作!我非常想要輕鬆地抓住span元素,我並沒有考慮使用不同的方法。謝謝! –