Python爬取图片

import wget
import requests
from bs4 import BeautifulSoup

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
		soup=BeautifulSoup(r.text,"html.parser")
		h=[]
		for link in soup.find_all('img'):
			h.append(link.get('src'))
		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/85058048