【问题标题】:Cannot gather attributes from span element using BeautifulSoup无法使用 BeautifulSoup 从 span 元素收集属性
【发布时间】:2018-01-08 17:59:12
【问题描述】:

This 是我想使用 BeautifulSoup 从以下站点 (https://wwwn.cdc.gov/nchs/nhanes/search/datapage.aspx?Component=Examination) 解析的源代码的图像。我希望提取 属性中的属性:htm 链接。

我的 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 元素的内容中我没有看到一个名为 'print-only' 的类)。

我注意到 span 属性在 Firefox 开发者窗口中显示为灰色。快速谷歌搜索显示这意味着该属性是隐藏的。是不是说明我用的方法无法获取?

【问题讨论】:

    标签: python html beautifulsoup


    【解决方案1】:

    由于 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
    

    【讨论】:

    • 这行得通!我非常想轻松抓取 span 元素,因此我没有考虑使用其他方法。谢谢!
    【解决方案2】:

    这是使用 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>
    

    请注意,您要查找的标签不存在。我展示了列表的第一项,以便您可以更好地分析您需要的网址在哪里,正如我们所见,这是我们想要的第一个 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']
    

    【讨论】:

      【解决方案3】:

      试试这个:

      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")
      

      【讨论】:

      • 看起来你只是在改变类的识别方式?如果是这样,我已经尝试过该语法无济于事。
      • 1) 我有一个错字 - findAll vs find_all 2) 我重写了它以确保 bs4 与 bs4 语法一起使用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      相关资源
      最近更新 更多