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)


展开阅读全文

没有更多推荐了,返回首页