Aug-22-2018, 11:39 PM
I'm looking at tutorial Web-scraping part-2 and have a question regarding this code:
I also ran code with
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.