2017-08-01 82 views
0

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屬性是灰色的。快速谷歌搜索顯示,這意味着該屬性是隱藏的。這是否意味着它不能用我使用的方法獲得?

回答

0

由於span元素被隱藏,因此您將無法使用BeautifulSoup檢索它。也許,你可以使用其他屬性來獲得你需要的鏈接。如果您知道想要提取鏈接的.htm文件的名稱,則可以使用內部文本簡單地找到'a'元素(它將綁定所需的鏈接和隱藏的span元素),然後僅提取從元素 'href' 屬性如下:

import requests 
from bs4 import BeautifulSoup 
import html5lib 
import string 

ascii = set(string.printable) 
def remove_non_ascii(s): 
    return filter(lambda x: x in ascii, s) 


url = 'https://wwwn.cdc.gov/nchs/nhanes/search/datapage.aspx?Component=Examination' 
home_url = 'https://wwwn.cdc.gov' 

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} 
page = requests.get(url, headers = headers, allow_redirects = True) 
soup = BeautifulSoup(remove_non_ascii(page.text), "html5lib") 

link = soup.find_all('a', text='ARX_F Doc')[0] 
complete_url = home_url + link.get('href') 
print complete_url 
+0

這工作!我非常想要輕鬆地抓住span元素,我並沒有考慮使用不同的方法。謝謝! –

0

這裏有一個解決方案,以獲得您所需要使用的是什麼BeautifulSoup,首先讓我們表:

table = soup.find("table",{'id':'GridView1'}) 

現在我們發現在它的身上tr標籤:

>>> table.find('tbody').findAll('tr')[0] 
<tr> 
       <td class="text-center"> 
        2009-2010 
       </td><td class="text-left">Arthritis Body Measures</td><td class="text-center"> 
        <a href="/Nchs/Nhanes/2009-2010/ARX_F.htm">ARX_F Doc</a> 
       </td><td class="text-center"> 
        <a href="/Nchs/Nhanes/2009-2010/ARX_F.XPT">ARX_F Data [XPT - 510.5 KB]</a> 
       </td><td class="text-center"> 
        September, 2011 
       </td> 
      </tr> 

注意標籤你正在尋找不存在。我發現列表中的第一個項目,所以你可以analise更好的地方是你需要,我們可以看到的網址,這是我們想要的第一a標籤,因此,例如:

>>> table.find('tbody').findAll('tr')[0].find('a') 
<a href="/Nchs/Nhanes/2009-2010/ARX_F.htm">ARX_F Doc</a> 

現在所有剩下要做的就是寫列表理解加入第一a標籤的所有href屬性在每個tr標籤的列表:

>>> trList = table.find('tbody').findAll('tr') 
>>> lst = [tr.find('a')['href'] for tr in trList] 

如果我們打印的lst我們看到的第一要素,這是我們所期望的輸出:

>>> lst[:3] 
['/Nchs/Nhanes/2009-2010/ARX_F.htm', '/Nchs/Nhanes/1999-2000/AUX1.htm', '/Nchs/Nhanes/2001-2002/AUX_B.htm'] 
0

試試這個:

import urllib.request                                    
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.find_all("span", class_="print-only") 
+0

看起來你只是在改變班級的識別方式?如果是這樣,我已經嘗試過這種語法無濟於事。 –

+0

1)我有一個拼寫錯誤 - findAll vs find_all 2)我重寫了它,以確保bs4與bs4語法一起使用。 – imjosh