Python 官方文档:入门教程 => 点击学习
使用python find函数和urllib下载图片。A:#!/usr/bin/env Python import time import urllib i = 0 url = ['']*10 name = ['']*10 con
使用python find函数和urllib下载图片。
A:
#!/usr/bin/env Python
import time
import urllib
i = 0
url = ['']*10
name = ['']*10
con = urllib.urlopen('/file/imgs/upload/202301/31/0bvfvqh1iqp.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)
--结束END--
本文标题: Python 批量下载图片示例
本文链接: https://lsjlt.com/news/186348.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0