Issue
I am trying to get all the names in each html line and store all of them together if they are in the same line. For example for the first line I would want [Finneas O'Connell, Rob Kinelski, John Greenhan] and the second one would be [Louis Bell, Frank Dukes, Manny Marroquin, Mike Bozzi] and so on.
[<a class="freelink freelink-nid freelink-internal" href="/grammys/artists/finneas-oconnell/251939" title="Click to view a local node.">Finneas O'Connell</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/rob-kinelski/251846" title="Click to view a local node.">Rob Kinelski</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/john-greenham/251779" title="Click to view a local node.">John Greenham</a>]
[<a class="freelink freelink-nid freelink-internal" href="/grammys/artists/louis-bell/243143" title="Click to view a local node.">Louis Bell</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/adam-feeney/18417" title="Click to view a local node.">Frank Dukes</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/manny-marroquin/14875" title="Click to view a local node.">Manny Marroquin</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/mike-bozzi/17767" title="Click to view a local node.">Mike Bozzi</a>]
[<a class="freelink freelink-nid freelink-internal" href="/grammys/artists/j-white-did-it/188876" title="Click to view a local node.">J. White Did It</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/eddie-%E2%80%9Cemix%E2%80%9D-hern%C3%A1ndez/298062" title="Click to view a local node.">Eddie “eMIX” Hernández</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/shawn-source-jarrett/298063" title="Click to view a local node.">Shawn "Source" Jarrett</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/jaycen-joshua/3518" title="Click to view a local node.">Jaycen Joshua</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/stuart-white/12821" title="Click to view a local node.">Stuart White</a>, <a class="freelink freelink-nid freelink-internal" href="/grammys/artists/colin-leonard/243375" title="Click to view a local node.">Colin Leonard</a>]
when I use the code below it just lists them all one by one for all of them:
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://www.grammy.com/grammys/awards/winners-nominees/138'
page = requests.get(url).text
soup = BeautifulSoup(page,'lxml')
category = soup.find_all('div', class_ = "view-grouping-content")
for i in category:
workers = i.find_all('div', class_="views-field views-field-body-1")
for f in workers:
k = f.findAll('a')
for a in k:
print(a.string)
I am just confused on how I would fix this issue
Solution
You can try this to get your expected output :
from bs4 import BeautifulSoup
import requests
url = 'https://www.grammy.com/grammys/awards/winners-nominees/138'
page = requests.get(url).text
soup = BeautifulSoup(page,'lxml')
category = soup.find_all('div', class_ = "view-grouping-content")
for i in category:
for f in i.find_all('div', class_="views-field views-field-body-1"):
k = f.findAll('a')
x=[]
for a in k:
x.append(a.text)
print(x)
We are just making new list (x)
that stores all workers name, and printing them one by one.
Answered By - Xitiz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.