发布时间:2019-08-26 07:20:06编辑:auto阅读(1663)
使用Python find函数和urllib下载图片。
A:
#!/usr/bin/env python
import time
import urllib
i = 0
url = ['']*10
name = ['']*10
con = urllib.urlopen('http://www.ithome.com/html/bizhi/164396.htm').read()
src = con.find(r'/newsuploadfiles')
end = con.find(r'.jpg',src)
name[0] = con[src +24 :end +1]
while src != -1 and end != -1 and i < 10:
url[0] = con[src -21 :end +4]
src = con.find(r'/newsuploadfiles',end)
end = con.find(r'.jpg',src)
content = urllib.urlopen(url[0]).read()
open(r'img/'+ name[0]+'jpg','w+').write(content)
name[0] = con[src +24 :end +1]
print url[0]
time.sleep(3)
i = i + 1
else:
print "Download Over!"B:
#!/usr/bin/env python
import re
import urllib
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = r'data-original="(.+?\.jpg)" />'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
i = 0
for imgurl in imglist:
name = imgurl[45:]
print name
content = urllib.urlopen(imgurl).read()
open(r'tmp/' + name,'w+').write(content)
i = i + 1
html = getHtml("http://www.ithome.com/html/bizhi/164396.htm")
print getImg(html)
上一篇: python base64解码TypeE
下一篇: 【python】 倒序输出字符串
51270
50709
41310
38127
32586
29494
28349
23210
23181
21505
1580°
2301°
1910°
1851°
2166°
1892°
2582°
4321°
4171°
2976°