Issue
I am trying to extract a list of anchor tag text within a class and append each text to different list using beautifulsoup as follows.
from urllib.request import Request
from bs4 import BeautifulSoup
data = """<p class="comments">
<a href="/search/?searchtype=name1$amp; query=x11">comment1</a>,
<a href="/search/?searchtype=name1$amp; query=x21">comment2 </a>
</p>
<p class="comments">
<a href="/search/?searchtype=name1$amp; query=x31">comment3</a>,
<a href="/search/?searchtype=name1$amp; query=x41">comment4 </a>
</p>"""
soup = BeautifulSoup(data, "html.parser")
data1 = soup.find_all("p", {"class": "comments"})
x1 = []
x2 = []
for data in data1:
get_a = data.find_all("a")
text = ""
for i in get_a:
text = i.text
x1.append(text)
x2.append(text)
print(x1)
print(x2)
I get the following results:
['comment1', 'comment2 ', 'comment3', 'comment4 ']
['comment1', 'comment2 ', 'comment3', 'comment4 ']
But, I would like to get the desired output as follows:
x1= ['comment1', 'comment3']
x2= ['comment2', 'comment4']
I appreciate you for your help in advance.
Solution
soup = BeautifulSoup(data, "html.parser")
data1 = soup.find("p", {"class": "comments"}).find_all('a')
data2 = soup.find_all("p", {"class": "comments"})[1].find_all('a')
x1 = []
x2 = []
for data in data1:
x1.append(data.text)
for data in data2:
x2.append(data.text)
print(x1)
print(x2)
Output:
['comment1', 'comment2 ']
['comment3', 'comment4 ']
Answered By - Sergey Karbivnichiy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.