实用代码Python(三):使用requests+wget爬取图片

代码

import wget
import requests
import re

def getURL(url):
	try:
		hd={'user-agent':'Mozallia/5.0'}
		r=requests.request('get',url,headers=hd)
		r.raise_for_status()
		r.encoding=r.apparent_encoding
		html=r.text
		h=re.findall(r"src=\".*.[jpg,png]\"",html)
		for i in range(len(h)):
			h[i]=h[i][5:-1]
		return h
	except:
		print("爬取失败")

def download(url):
	for href in url:
		try:
			wget.download(href,r"D:\filepython\ipython\pic\{}".format(href[-7:]))
		except Exception:
			continue

	
if __name__=="__main__":
	url="http://www.tooopen.com/view/176601.html"
	href=getURL(url)
	download(href)


猜你喜欢

转载自blog.csdn.net/weixin_40775077/article/details/84559827
今日推荐