python网络数据采集百度云_python网络数据采集11 下载所有src链接

[python]代码库from urllib.request import urlretrieve

from urllib.request import urlopen

from bs4 import BeautifulSoup

import os

downloadDirectory = "download"

baseUrl = "http://www.pythonscraping.com/"

def getAbsoluteURL(baseUrl,source):

if source.startswith("http://www."):

url = "http://"+source[11:] # 例如http://orielly.com

elif source.startswith("http://"):

url = source

elif source.startswith("www."):

url = "http://"+source[4:]

else:

url = baseUrl+"/"+source

if baseUrl not in url:

return None

return url

def getDownloadPath(baseUrl,absoluteUrl,downloadDirectory):

path = absoluteUrl.replace("www.","") # 例如 http://orielly.com

path = path.replace(baseUrl,"") # 去掉http://www.pythonscraping.com/

path = downloadDirectory + path

directory = os.path.dirname(path) # 获取文件路径所在的目录

if not os.path.exists(directory): # os.path.exists是否存在次目录,存在就返回True

os.mkdir(directory)

return path

html = urlopen("http://www.pythonscraping.com/")

bsObj = BeautifulSoup(html,'html.parser')

downloadList = bsObj.findAll(src = True) # 找到所有的src的值?

for download in downloadList:

fileUrl = getAbsoluteURL(baseUrl,download["src"])

if fileUrl is not None:

print(fileUrl)

urlretrieve(fileUrl,getDownloadPath(baseUrl,fileUrl,downloadDirectory))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值