Issue
I have this problem it may look easy but I just can't do it, I am working on a web scraping project and now I am scraping a list of keywords so my code prints the results just like this.
Kids Wear Shop
Plastic Children Toys
Balloon
Costumes
Montessori Toys
all I want to do to store these results in a single list I tried to append them to a list but it stores every single line in a list of its own.
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
for div in soup.find_all('div', class_='keywords content-div'):
for span in div.find_all('span', class_='keyword key-content'):
for a in span.find_all('a'):
print(a.text)
Solution
find_all()
returns a list of all results. So if there is only one found it will return a list which contains that one result. If you know there is only one value then use find()
instead. You can then append that single value:
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
lst = []
for div in soup.find_all('div', class_='keywords content-div'):
for span in div.find_all('span', class_='keyword key-content'):
lst.append(span.find('a').text)
print(lst)
Or if there are indeed multiple a
s possible you can just append everything to the list:
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
lst = []
for div in soup.find_all('div', class_='keywords content-div'):
for span in div.find_all('span', class_='keyword key-content'):
for a in span.find_all('a'):
lst.append(a.text)
print(lst)
Or you concatenate the lists in every step:
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
lst = []
for div in soup.find_all('div', class_='keywords content-div'):
for span in div.find_all('span', class_='keyword key-content'):
lst += [a.text for a in span.find_all('a')]
print(lst)
Answered By - ewz93
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.