简体   繁体   中英

Beautiful Soup: Get text data from html

Here is my html code now I want extract data from following html code using beautiful soup

<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?

Since the find_all returns a list of options, you can use list comprehensions to obtain the answer as you expect

>>> 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 from a_list and appends them.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM