Issue
I have a block of html code. I need to take Name 1 and Name 2 and links from this code separately. As a result I want dictionary.
{"red" : {"name" : Name1, url: "http://link1.com"}, "blue": {"name" : Name2, "url": "http://link2.com"}}
But in this code I can't understand how I can catch Name2 and link2
I'm using Python and Beautiful soap with Requests lib Could you please help me, thank you.
<td style="width:100px" class="b-fight-details__table-col l-page_align_left">
<p class="b-fight-details__table-text">
<a class="b-link b-link_style_black" href="http://link1.com">
Name 1
</a>
</p>
<p class="b-fight-details__table-text">
<a class="b-link b-link_style_black" href="http://link2.com">
Name 2
</a>
</p>
Solution
You can try this:
from BeautifulSoup import BeautifulSoup
import requests
html = requests.get(yoururl).text
yourdict = {}
soup = BeautifulSoup(html)
for a in soup.find_all('a', class_="b-link b-link_style_black"):
print("Name:", a.text, "\n link:" a['href'])
yourdict['red'] = {'name':a.text, 'link':a['href']}
Answered By - Matteo Bianchi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.