Jf2k
Jf2k

Reputation: 119

Beautiful Soup and URL base names in python

How would you use Beautiful soup to get a URL base name in python? Given the url name as a string, what would you do?

Upvotes: 0

Views: 386

Answers (2)

Jason Champion
Jason Champion

Reputation: 2750

I'd use urlparse over BeautifulSoup for extracting pieces of a URL. Here's an example:

from urlparse import urlparse

parsedurl = urlparse('http://example.com/filename.txt')
print parsedurl.path

The output will be:

/filename.txt

Upvotes: 2

shaktimaan
shaktimaan

Reputation: 12092

If by base name you mean, given http://example.com/file.txt you want file.txt? In that case you do not need Beautiful Soup at all. Simple string manipulation code would work.

It is also known that os.path.basename('http://example.com/file.txt) would give you file.txt

Upvotes: 3

Related Questions