Beautiful soup: getting text data from html
Here is my html code, now I want to extract data from the following HTML using nice 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 lovely soup code
soup = BeautifulSoup(html_doc)
for option in soup.find_all('td', attrs={'class':"td-option"}):
print option.text
output of the above code:
A.
120 m
B.
240 m
C.
300 m
D.
None of these
but i want the following output
A.120 m
B.240 m
C.300 m
D.None of these
What should I do?
+3
source to share
2 answers
Since it find_all
returns a list of parameters, you can use lists to get 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 is he doing?
-
[ a_list[i] + a_list[i+1] for i in range(0,len(a_list),2) ]
Takes adjacent items froma_list
and adds them.
+1
source to share
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
0
source to share