我正试图从这个html部分中分别删除2,768和25,000:
<td class="ColCompany">Company</td>
<td class="alignCenter">2,768</td><td class="alignCenter" >
<a class="aMeasure" title="Text. href="/Reports/Index#Measure"> 69 </a></td>
<td class="alignCenter">25,000</td>
<td class="alignCenter">7</td>
使用以下python代码:
def get_posts():
global Comp_Name
Comp_Name=""
plain_text = r.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll('td',{'class': 'alignCenter'}):
title = link.string
if title != None :
list_of_titles.append(title)
不幸的是,他一并返回了两个值
我很乐意为您提供帮助,以便每个数字都分开
10x
答案 0 :(得分:0)
要获取这两个数字,可以使用以下脚本:
data = ''' <td class="ColCompany">Company</td>
<td class="alignCenter">2,768</td><td class="alignCenter" >
<a class="aMeasure" title="Text. href="/Reports/Index#Measure"> 69 </a></td>
<td class="alignCenter">25,000</td>
<td class="alignCenter">7</td>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'html.parser')
numbers = [t.get_text(strip=True) for t in soup.select('.alignCenter')]
print(numbers[0])
print(numbers[2])
打印:
2,768
25,000
答案 1 :(得分:0)
基于提供的html,您也许可以使用nth-of-type
。尽管访问两次似乎比仅索引两个列表的效率低。
soup.select_one('td.alignCenter:nth-of-type(2)').text
和
soup.select_one('td.alignCenter:nth-of-type(3)').text
nth-of-type
索引来自在html上使用jsoup进行测试并添加了周围的表格标记。您的里程可能会有所不同,但原理是相同的。