python wget_python wget下载文件处理的一些问题
wget的安裝 由于嘗試pip安裝一直失敗,下載了wget3.2的數據包。 解壓后運行python setup.py install即可完成安裝。
基本使用的下載方法 import wget wget.download(downloadURL,filepathandname)
關于下載鏈接地址的獲取 目前使用的方法:利用urllib得到網頁數據,然后規則匹配得到相應的鏈接。 利用urllib獲取(是否有更好的方法,BeautifulSoup?):
import urllib
from urllib import request
import re
pageRequest = request.urlopen(driver.current_url)
pageRead = pageRequest.read().decode('utf-8')
#為什么需要decode?
#在python3.0中 pageRequest.read()返回字節型數據,而re模塊需要字符串
#系統提示錯誤can't use a string pattern on a bytes-like object
for eachline in pageRead.split('\n'):
webDownloadURL = re.findall('src="(.+)"',eachline)
if(len(webDownloadURL)>0) and re.search('iframe',eachline):
wgetURL = webDownloadURL[0]
print('%s'%wgetURL)
其他下載文件的方法
#1、文件存儲形式
filedownload=urllib2.urlopen(url)
urldata=filedownload.read()
fwrite=open(path,'wb')
fwrite.write(urldata)
fwrite.close()
#2、urllib.urlretrieve
urllib.urlretrieve(url, filename)
關于文件系統的處理
文件主要使用到的包:os,shutil;判斷文件是否存在:os.path.exists(‘….’)
#判斷文件夾是否存在,不存在建立新的文件夾
import os
import shutil
if os.path.exists(prefixpathname):
pass
else:
os.mkdir(prefixpathname)
#刪除文件
os.rmdir(...)
os.remove(...)
#由于os.rmdir無法刪除一個包含其他文件夾的文件夾,而shutil具有這樣的功能
shutil.rmtree(...)
總結
以上是生活随笔為你收集整理的python wget_python wget下载文件处理的一些问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python wget_python w
- 下一篇: JS基础-DOM增删改-尚硅谷视频p10