簡體   English   中英

美麗的湯:從HTML獲取文本數據

[英]Beautiful Soup: Get text data from html

這是我的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>

這是我美麗的湯碼

soup = BeautifulSoup(html_doc)
for option in soup.find_all('td', attrs={'class':"td-option"}):
    print option.text

以上代碼的輸出:

A.
120 m
B.
240 m
C.
300 m
D.
None of these

但我想要跟隨輸出

A.120 m
B.240 m
C.300 m
D.None of these

我該怎么辦?

由於find_all返回一個選項列表,您可以使用列表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

它能做什么?

  • [ a_list[i] + a_list[i+1] for i in range(0,len(a_list),2) ]a_list獲取相鄰元素並附加它們。
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)

A.120 m
B.240 m
C.300 m
D.None of these

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM