- Python Web Scraping Cookbook
- Michael Heydt
- 111字
- 2021-06-30 18:44:12
How it works
In the constructor for URLUtility, there is a call to urlib.parse.urlparse. The following demonstrates using the function interactively:
>>> parsed = urlparse(const.ApodEclipseImage())
>>> parsed
ParseResult(scheme='https', netloc='apod.nasa.gov', path='/apod/image/1709/BT5643s.jpg', params='', query='', fragment='')
The ParseResult object contains the various components of the URL. The path element contains the path and the filename. The call to the .filename_without_ext property returns just the filename without the extension:
@property
def filename_without_ext(self):
filename = os.path.splitext(os.path.basename(self._parsed.path))[0]
return filename
The call to os.path.basename returns only the filename portion of the path (including the extension). os.path.splittext() then separates the filename and the extension, and the function returns the first element of that tuple/list (the filename).
推薦閱讀
- EDA技術(shù)與VHDL編程
- SEO 20日
- Spring Boot 2.0 Projects
- Hands-On Chatbots and Conversational UI Development
- Learning QGIS 2.0
- TCP/IP入門經(jīng)典(第5版)
- OpenLayers Cookbook
- Spring Cloud微服務(wù)架構(gòu)進階
- Spring 5.0 Projects
- 6G新技術(shù) 新網(wǎng)絡(luò) 新通信
- 網(wǎng)絡(luò)AI+:2030后的未來網(wǎng)絡(luò)
- Selenium WebDriver 3 Practical Guide
- 華為HCIA-Datacom認證指南
- Cisco無線局域網(wǎng)配置基礎(chǔ)
- 網(wǎng)絡(luò)空間作戰(zhàn):機理與籌劃