Here is my html code now I want extract data from following html code using beautiful soup
这是我的html代码,现在我希望使用美丽的汤从以下html代码中提取数据
<tr class="tr-option">
<td class="td-option"><a href="">A.</a></td>
<td class="td-option">120 m</td>
<td class="td-option"><a href="">B.</a></td>
<td class="td-option">240 m</td>
<td class="td-option"><a href="">C.</a></td>
<td class="td-option" >300 m</td>
<td class="td-option"><a href="">D.</a></td>
<td class="td-option" >None of these</td>
</tr>
here is my beautiful soup code
这是我美丽的汤代码
soup = BeautifulSoup(html_doc)
for option in soup.find_all('td', attrs={'class':"td-option"}):
print option.text
output of above code:
以上代码的输出:
A.
120 m
B.
240 m
C.
300 m
D.
None of these
but I want following output
但我想要跟随输出
A.120 m
B.240 m
C.300 m
D.None of these
What should I do?
我该怎么办?
2 个解决方案
#1
1
Since the find_all
returns a list of options, you can use list comprehensions to obtain the answer as you expect
由于find_all返回选项列表,因此您可以使用列表推导来获得您期望的答案
>>> a_list = [ option.text for option in soup.find_all('td', attrs={'class':"td-option"}) ]
>>> new_list = [ a_list[i] + a_list[i+1] for i in range(0,len(a_list),2) ]
>>> for option in new_list:
... print option
...
A.120 m
B.240 m
C.300 m
D.None of these
What it does?
它能做什么?
-
[ a_list[i] + a_list[i+1] for i in range(0,len(a_list),2) ]
Takes adjacent elements froma_list
and appends them.
[a_list [i] + a_list [i + 1] for i in range(0,len(a_list),2)]从a_list中获取相邻元素并附加它们。
#2
0
soup = BeautifulSoup(html_doc)
options = soup.find_all('td', attrs={'class': "td-option"})
texts = [o.text for o in options]
lines = []
# Add every two-element pair as a concatenated item
for a, b in zip(texts[0::2], texts[1::2]):
lines.append(a + b)
for l in lines:
print(l)
Gives
A.120 m
B.240 m
C.300 m
D.None of these
#1
1
Since the find_all
returns a list of options, you can use list comprehensions to obtain the answer as you expect
由于find_all返回选项列表,因此您可以使用列表推导来获得您期望的答案
>>> a_list = [ option.text for option in soup.find_all('td', attrs={'class':"td-option"}) ]
>>> new_list = [ a_list[i] + a_list[i+1] for i in range(0,len(a_list),2) ]
>>> for option in new_list:
... print option
...
A.120 m
B.240 m
C.300 m
D.None of these
What it does?
它能做什么?
-
[ a_list[i] + a_list[i+1] for i in range(0,len(a_list),2) ]
Takes adjacent elements froma_list
and appends them.
[a_list [i] + a_list [i + 1] for i in range(0,len(a_list),2)]从a_list中获取相邻元素并附加它们。
#2
0
soup = BeautifulSoup(html_doc)
options = soup.find_all('td', attrs={'class': "td-option"})
texts = [o.text for o in options]
lines = []
# Add every two-element pair as a concatenated item
for a, b in zip(texts[0::2], texts[1::2]):
lines.append(a + b)
for l in lines:
print(l)
Gives
A.120 m
B.240 m
C.300 m
D.None of these