从一个用靓汤制作的网站上筛选返回的列表

2024-05-17 02:54:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python编写代码。我一直在网上搜寻nba选秀的名字、球队形象和大学前景。但是当我刮取大学的名字时,我会同时看到学院页面和学院名称。我怎样才能得到它,这样我就只能看到大学了?我试过在锚点的末尾添加.string和.text(锚定.string). 在

import urllib2
from BeautifulSoup import BeautifulSoup
# or if your're using BeautifulSoup4:
# from bs4 import BeautifulSoup

list = []
soup = BeautifulSoup(urllib2.urlopen(
                            'http://www.cbssports.com/nba/draft/mock-draft'
                             ).read()
                     )

rows = soup.findAll("table",
                    attrs = {'class':'data borderTop'})[0].tbody.findAll("tr")[2:]

for row in rows:
  fields = row.findAll("td")
  if len(fields) >= 3:
    anchor = row.findAll("td")[2].findAll("a")[1:]
    if anchor:
      print anchor

Tags: fromimportstringifurllib2名字大学学院
2条回答

html中锚的格式是<a href='web_address'>Text-that-is-displayed</a>,所以除非已经有了一个奇特的html解析器库(我敢打赌有,只是不知道有什么),否则您可能需要使用某种正则表达式来解析出所需的锚定部分。在

而不是仅仅:

print anchor

使用:

^{pr2}$

相关问题 更多 >