<h3 id="LABandServerNamingConvention-:"><a href="/display/ES/Lab+Org+Code+Summary+Listing">Lab Org Code Summary Listing</a>:</h3>
<div class="sectionColumnWrapper">
<div class="sectionMacro">
<div class="sectionMacroRow">
<div class="columnMacro">
<div class="table-wrap">
<table class="confluenceTable">
<tbody>
<tr>
<th class="confluenceTh">
<p>Prefix</p>
</th>
<th class="confluenceTh">
<p>Group</p>
</th>
<th class="confluenceTh">
<p>Contact</p>
</th>
<th class="confluenceTh">
<p>Dev/Test Lab</p>
</th>
<th class="confluenceTh">
<p>Performance</p>
</th>
</tr>
<tr>
<td class="confluenceTd">
<p>SEE00</p>
</td>
<td class="confluenceTd">
<p>Entertainment</p>
</td>
<tr><td class="confluenceTd"><p>SEF00</p></td><td class="confluenceTd"><p>APTRA Vision</p></td><td class="confluenceTd"><p> </p></td><td class="confluenceTd"><p><a href="/pages/viewpage.action?pageId=83909590">VCD Lab</a> , <a href="/display/ES/SEF00+%28+Aptra+Vision%29+-+Virtual+Lab+Details">Test Lab</a></p></td>
我有一个包含5列的表格,其中2列已填写此特定条目。 如何从表格中将行数据从此HTML片段中获取到我的python代码中。我正在使用BeautifulSoup。这是我到目前为止所尝试的:
data = requests.get(url,auth=(username,password))
sample = data.content
soup = BeautifulSoup(sample,'html.parser')
article_text = ' '
article = soup.findAll('td', {'class' : "confluenceTd" })
for element in article:
article_text += '\n' + ''.join(element.findAll(text = True))
我想以某种方式获得&#39; SEE00&#39;和&#39;娱乐&#39;
答案 0 :(得分:1)
from bs4 import BeautifulSoup
doc = '''<h3 id="LABandServerNamingConvention-:"><a href="/display/ES/Lab+Org+Code+Summary+Listing">Lab Org Code Summary Listing</a>:</h3>
<div class="sectionColumnWrapper"><div class="sectionMacro"><div class="sectionMacroRow"><div class="columnMacro"><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th class="confluenceTh"><p>Prefix</p></th><th class="confluenceTh"><p>Group</p></th><th class="confluenceTh"><p>Contact</p></th><th class="confluenceTh"><p>Dev/Test Lab</p></th><th class="confluenceTh"><p>Performance</p></th></tr><tr><td class="confluenceTd"><p>SEE00</p></td><td class="confluenceTd"><p>Entertainment</p></td>
'''
soup = BeautifulSoup(doc, 'lxml')
for row in soup.find_all('tr'):
print(row.get_text(separator='\t')) # this separator is only for format, you can use whatever you want
出:
Prefix Group Contact Dev/Test Lab Performance
SEE00 Entertainment
你可以用切片来控制for循环:
for row in soup.find_all('tr')[1:]:
这只会打印
SEE00 Entertainment
更新
在:
for row in soup.find_all('tr'):
row_data = row.get_text(strip=True, separator='|').split('|')[:2]
print(row_data)
出:
['Prefix', 'Group']
['SEE00', 'Entertainment']
['SEF00', 'APTRA Vision']