how to extract page-URL using BeautifulSoup

MRAB python at mrabarnett.plus.com
Thu Oct 31 13:36:25 EDT 2013


On 31/10/2013 15:59, bhaktanishant at gmail.com wrote:
> I want to extract the page-url. for example:
> if i have this code
>
> import urllib2
> from bs4 import BeautifulSoup
> link = "http://www.google.com"
> page = urllib2.urlopen(link).read()
> soup = BeautifulSoup(page)
>
> then i can extract title of page by:
>
> title = soup.title
>
> but i want to know that how to extract page-URL from "soup" that will be "http://www.google.com"
>
Have a look at what you're passing to BeautifulSoup (save it to a file
and look at it in an editor). It's HTML. Does it contain anything that
says where it came from? No. So BeautifulSoup can't know either.

All BeautifulSoup does is parse the HTML that it's given.



More information about the Python-list mailing list