由于今天工作早早做完,就想从网上找一些偶像的照片等写自己的网站使用。然后又正在学习python就想着扒一些图片。但是发现有时候网页上出现中文就不行,后来还是解决了,棒棒的。
import urllib.request
import re
from urllib.request import quote, unquote
url = "https://baike.baidu.com/item/刘亦菲/136156?fr=aladdin" #http://findicons.com/pack/2787/beautiful_flat_icons
# utf8编码,指定安全字符。
url = quote(url, safe=";/?:@&=+$,", encoding="utf-8")
webPage=urllib.request.urlopen(url) #https://baike.baidu.com/item/刘亦菲/136156?fr=aladdin
#print (webPage)
data = webPage.read()
data = data.decode('UTF-8')
#print (data)
k = re.split(r'\s+',data)
s = []
sp = []
si = []
for i in k :
if (re.match(r'src',i) or re.match(r'href',i)):
if (not re.match(r'href="#"',i)):
if (re.match(r'.*?png"',i) or re.match(r'.*?ico"',i) or re.match(r'.*?jpg"',i) ):
if (re.match(r'src',i)):
s.append(i)
for it in s :
if (re.match(r'.*?png"',it)):
sp.append(it)
for it in s :
if (re.match(r'.*?jpg"',it)):
sp.append(it)
cnt = 0
cou = 1
for it in sp:
m = re.search(r'src="(.*?)"',it)
iturl = m.group(1)
print(iturl)
if (iturl[0]=='/'):
continue;
web = urllib.request.urlopen(iturl)
itdata = web.read()
if (cnt%3==1 and cnt>=4 and cou<=30):
f = open('d:/image/'+str(cou)+'.png',"wb")
cou = cou+1
f.write(itdata)
f.close()
print(it)
cnt = cnt+1
如图所示:
截图.png
刚学python,有什么问题欢迎交流。