使用Python3編寫(xiě)抓取網(wǎng)頁(yè)和只抓網(wǎng)頁(yè)圖片的腳本
最基本的抓取網(wǎng)頁(yè)內(nèi)容的代碼實(shí)現(xiàn):
#!/usr/bin/env python from urllib import urlretrieve def firstNonBlank(lines): for eachLine in lines: if not eachLine.strip(): continue else: return eachLine def firstLast(webpage): f = open(webpage) lines = f.readlines() f.close() print firstNonBlank(lines), lines.reverse() print firstNonBlank(lines), def download(url='http://www',process=firstLast): try: retval = urlretrieve(url)[0] except IOError: retval = None if retval: process(retval) if __name__ == '__main__': download()
利用urllib模塊,來(lái)實(shí)現(xiàn)一個(gè)網(wǎng)頁(yè)中針對(duì)圖片的抓取功能:
import urllib.request import socket import re import sys import os targetDir = r"C:\Users\elqstux\Desktop\pic" def destFile(path): if not os.path.isdir(targetDir): os.mkdir(targetDir) pos = path.rindex('/') t = os.path.join(targetDir, path[pos+1:]) return t if __name__ == "__main__": hostname = "http://www.douban.com" req = urllib.request.Request(hostname) webpage = urllib.request.urlopen(req) contentBytes = webpage.read() for link, t in set(re.findall(r'(http:[^\s]*?(jpg|png|gif))', str(contentBytes))): print(link) urllib.request.urlretrieve(link, destFile(link))
import urllib.request import socket import re import sys import os targetDir = r"H:\pic" def destFile(path): if not os.path.isdir(targetDir): os.mkdir(targetDir) pos = path.rindex('/') t = os.path.join(targetDir, path[pos+1:]) #會(huì)以/作為分隔 return t if __name__ == "__main__": hostname = "http://www.douban.com/" req = urllib.request.Request(hostname) webpage = urllib.request.urlopen(req) contentBytes = webpage.read() match = re.findall(r'(http:[^\s]*?(jpg|png|gif))', str(contentBytes) )#r'(http:[^\s]*?(jpg|png|gif))'中包含兩層圓括號(hào),故有兩個(gè)分組, #上面會(huì)返回列表,括號(hào)中匹配的內(nèi)容才會(huì)出現(xiàn)在列表中 for picname, picType in match: print(picname) print(picType) ''''' 輸出: http://img3.douban.com/pics/blank.gif gif http://img3.douban.com/icon/g111328-1.jpg jpg http://img3.douban.com/pics/blank.gif gif http://img3.douban.com/icon/g197523-19.jpg jpg http://img3.douban.com/pics/blank.gif gif ... '''
- Python爬蟲(chóng)抓取指定網(wǎng)頁(yè)圖片代碼實(shí)例
- Python3簡(jiǎn)單爬蟲(chóng)抓取網(wǎng)頁(yè)圖片代碼實(shí)例
- Python爬蟲(chóng)之網(wǎng)頁(yè)圖片抓取的方法
- python抓取網(wǎng)頁(yè)中鏈接的靜態(tài)圖片
- Python之多線程爬蟲(chóng)抓取網(wǎng)頁(yè)圖片的示例代碼
- Python實(shí)現(xiàn)簡(jiǎn)單網(wǎng)頁(yè)圖片抓取完整代碼實(shí)例
- Python使用正則表達(dá)式抓取網(wǎng)頁(yè)圖片的方法示例
- python抓取網(wǎng)頁(yè)中圖片并保存到本地
- Python抓取網(wǎng)頁(yè)圖片難點(diǎn)分析
相關(guān)文章
Python讀取mat文件,并轉(zhuǎn)為csv文件的實(shí)例
今天小編就為大家分享一篇Python讀取mat文件,并轉(zhuǎn)為csv文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07python與js主要區(qū)別點(diǎn)總結(jié)
在本篇內(nèi)容里小編給大家整理了關(guān)于python與js主要區(qū)別點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-09-09詳解Python如何獲取視頻文件的大小和時(shí)長(zhǎng)
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)獲取視頻文件的大小和時(shí)長(zhǎng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03python 對(duì)dataframe下面的值進(jìn)行大規(guī)模賦值方法
今天小編就為大家分享一篇python 對(duì)dataframe下面的值進(jìn)行大規(guī)模賦值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06詳解OpenCV自適應(yīng)直方圖均衡化的應(yīng)用
在本文中,將介紹如何應(yīng)用對(duì)比度受限的自適應(yīng)直方圖均衡化 ( Contrast Limited Adaptive Histogram Equalization, CLAHE ) 來(lái)均衡圖像,需要的可以參考一下2022-02-02python time.strptime格式化實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python time.strptime格式化實(shí)例詳解內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02Python實(shí)現(xiàn)雙X軸雙Y軸繪圖的示例詳解
這篇文章主要介紹了如何利用fig.add_subplot和axes.twinx().twiny()方法實(shí)現(xiàn)雙X軸雙Y軸繪圖,文中的示例代碼講解詳細(xì),快跟隨小編一起動(dòng)手嘗試一下吧2022-04-04python雙端隊(duì)列原理、實(shí)現(xiàn)與使用方法分析
這篇文章主要介紹了python雙端隊(duì)列原理、實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了Python雙端隊(duì)列的概念、原理、定義及使用方法,需要的朋友可以參考下2019-11-11