Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web scraping: os.path.basename
#1
I'm looking at tutorial Web-scraping part-2 and have a question regarding this code:

import requests
from bs4 import BeautifulSoup
import webbrowser
import os

url = 'http://xkcd.com/1/'
url_get = requests.get(url)
soup = BeautifulSoup(url_get.content, 'lxml')
text = soup.select_one('#ctitle').text
link = soup.find('div', id='comic').find('img').get('src')
link = link.replace('//', 'http://')

# Image title and link
print(f'{text}\n{link}')

# Download image
img_name = os.path.basename(link)
img = requests.get(link)              
with open(img_name, 'wb') as f_out:   
	f_out.write(img.content)          

# Open image in browser or default image viewer
webbrowser.open_new_tab(img_name)
Why is img_name = os.path.basename(link) added? Is that a better practise from some reason?
I also ran code with webbrowser.open_new_tab([b]link[/b]) and it works. Also, script works without lines 17-20.
Reply


Messages In This Thread
Web scraping: os.path.basename - by Truman - Aug-22-2018, 11:39 PM
RE: Web scraping: os.path.basename - by Larz60+ - Aug-23-2018, 12:58 AM
RE: Web scraping: os.path.basename - by Truman - Aug-23-2018, 09:53 PM
RE: Web scraping: os.path.basename - by snippsat - Aug-23-2018, 10:16 PM
RE: Web scraping: os.path.basename - by Truman - Aug-23-2018, 11:22 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020