Starting from an Html input like this:
<p>
<a href="http://www.foo.com">this if foo</a>
<a href="http://www.bar.com">this if bar</a>
</p>
using BeautifulSoup, i would like to change this Html in:
<p>
<a href="http://www.foo.com">this if foo[1]</a>
<a href="http://www.bar.com">this if bar[2]</a>
</p>
saving parsed links in a dictionary with a result like this:
links_dict = {"1":"http://www.foo.com","2":"http://www.bar.com"}
Is it possible to do this using BeautifulSoup? Any valid alternative?
This should be easy in Beautiful Soup.
Something like:
Result of executing this on your text:
The only problem I can foresee with this solution is if your link text contains subtags; then you couldn’t do
''.join(link_tag.contents); instead you would need to navigate to the rightmost text element.