【发布时间】:2013-05-22 22:53:59
【问题描述】:
假设我有:
<tr>
<td class="prodSpecAtribute">word</td>
<td colspan="5">
<a href="http://www.cmegroup.com/clearing/trading-practices/CMEblock-trade.html" target="_blank">another_word</a>
</td>
</tr>
我想提取 2 个 td 类中的文本(word 和 another_word: 所以我用了 BeautifulSoup:
这是 Matijn Pieters 要求的代码: 基本上,它从 html 页面(从表中)获取信息并将这些值存储在左右列列表中。然后,我根据这些详细信息创建一个字典(使用左列列表作为键,对于值,我使用右列列表)
def get_data(page):
soup = BeautifulSoup(page)
left = []
right = []
#Obtain data from table and store into left and right columns
#Iterate through each row
for tr in soup.findAll('tr'):
#Find all table data(cols) in that row
tds = tr.findAll('td')
#Make sure there are 2 elements, a col and a row
if len(tds) >= 2:
#Find each entry in a row -> convert to text
right_col = []
inp = []
once = 0
no_class = 0
for td in tds:
if once == 0:
#Check if of class 'prodSpecAtribute'
if check(td) == True:
left_col = td.findAll(text=True)
left_col_x = re.sub('&\w+;', '', str(left_col[0]))
once = 1
else:
no_class = 1
break
else:
right_col = td.findAll(text=True)
right_col_x = ' '.join(text for text in right_col if text.strip())
right_col_x = re.sub('&\w+;', '', right_col_x)
inp.append(right_col_x)
if no_class == 0:
inps = '. '.join(inp)
left.append(left_col_x)
right.append(inps)
#Create a Dictionary for left and right cols
item = dict(zip(left, right))
return item
【问题讨论】:
-
正在复制链接中的哪些信息?
-
您的示例生成
[u'word']和[u'\n', u'another_word', u'\n'],后者包括a标记围绕 的空格。这就是你看到的吗? -
只是
<td>标签之间的文本 -
请注意,您的循环每次替换
col;原始列表中没有附加任何内容。为此使用col.extend(td.findAll(text=True))。 -
您正在向它询问
<td>标签内的所有文本元素;是否要过滤掉仅包含空格的文本?
标签: python html-parsing beautifulsoup