python - 如何使用Python BeautifulSoup提取td HTML标签?

标签 python web-scraping beautifulsoup

我正在尝试废弃 webpage并从中提取前缀及其名称。但是,对于某些标签,我无法提取它们,我的猜测是存在不可见的标签。这是我的Python代码:

opener.addheaders = [('User-agent', 'Mozilla/5.0')]
response = opener.open('http://bgp.he.net/AS23028#_prefixes')
html = response.read()
soup = BeautifulSoup(html)
soup_1 = soup.find("table", id = "table_prefixes4")
soup_2 = soup_1.findAll("td")
print soup_2

有人知道如何获取标签后的名称吗?以下是该页面的 html 内容:

<div class="flag alignright floatright"><img alt="United States" src="/images/flags/us.gif?1282328089" title="United States"/></div>
</td>, <td class="nowrap">
<a href="/net/209.176.111.0/24">209.176.111.0/24</a>
</td>, <td>Savvis

我想从 HTML 中提取前缀“209.176.111.0/24”和“Savvis”

最佳答案

数据就在那里;页面中没有丢失任何内容。 HTML 似乎没有被破坏(足以)导致标签丢失,也没有任何 JavaScript 改变浏览器中的页面:

for row in soup.select('table#table_prefixes4 tr'):
    print row.get_text(' - ', strip=True)

打印整个表格,包括标题。

仅获取单元格:

for row in soup.select('table#table_prefixes4 tr'):
    cells = row.find_all('td')
    if not cells:
        continue
    print [cell.get_text(strip=True) for cell in cells]

后者产生:

>>> for row in soup.select('table#table_prefixes4 tr'):
...     cells = row.find_all('td')
...     if not cells:
...         continue
...     print [cell.get_text(strip=True) for cell in cells]
... 
[u'38.229.0.0/16', u'PSINet, Inc.']
[u'38.229.0.0/19', u'PSINet, Inc.']
[u'38.229.32.0/19', u'PSINet, Inc.']
[u'38.229.64.0/19', u'PSINet, Inc.']
[u'38.229.128.0/17', u'PSINet, Inc.']
[u'38.229.252.0/22', u'PSINet, Inc.']
[u'68.22.187.0/24', u'AS23028.NET']
[u'192.138.226.0/24', u'Computer Systems Consulting Services']
[u'203.28.18.0/24', u'Information Technology Services']
[u'204.74.64.0/24', u'SAUNET']
[u'209.176.111.0/24', u'Savvis']
[u'216.90.108.0/24', u'Savvis']

关于python - 如何使用Python BeautifulSoup提取td HTML标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27443848/

相关文章:

python - 将颜色条添加到现有轴

python - 从网页上抓取 2 个不同格式的表格 - Beautiful Soup

python-3.x - 为什么这在我的request.get变量上显示为语法错误?

python - Scipy:通过 cdist 计算标准化欧几里德

python - (Excel) 访问 CodeModule 时 VBA 崩溃

python - 卡住名单副本

python - 通过使用 Beautiful Soup、Selenium 和 Pandas 提取 div 类中的值来抓取价格

python - Glassdoor 网页抓取与 Selenium

javascript - 当我使用 Nightmare 时,在页面之间移动并进行抓取

python - 使用 python beautiful soup 进行网络抓取的空值