本例为Python配合BeautifulSoup读取网络图片,并保存在本地。

BeautifulSoup可代替正则表达式,更好地解析Html文本,获取其中的指定内容,如Tag、Property等

# -*- coding: gbk -*-

import urllib
import urllib2
from bs4 import BeautifulSoup
import time
import re
import os,sys
import chardet def req(url):
#url='http://www.szu.edu.cn/2014/news/index_1.html'
header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
req=urllib2.Request(url,headers=header)
data=urllib.urlopen(req).read()
print data
return data def reqImg():
#url='http://www.junmeng.com/tj/22376_4.html'
url=r'http://www.junmeng.com/tj/22376.html'
header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
patnLink=r'<a href=".*/tj/22376_\d*.html"><img src.+</a>'
patnImg=r'<img src=.+>'
savedir=r'C:\Users\hp\Desktop\results'
if not os.path.exists(savedir):
os.mkdir(savedir) for i in range(1,20):
if i==1:
tempurl=url
else:
tempurl='http://www.junmeng.com/tj/22376_%d.html'%i
print tempurl
#req=Request(tempurl,headers=header)
data=urllib.urlopen(tempurl).read()
#print data
if i==19:
patnLink=r'<a href=.*><img src=.*</a>'
imgLinks=re.findall(patnLink,data)
#print results
link=imgLinks[0]
#print link
imgLink=link[link.find('src=')+5:link.find('.jpg')+4]
print imgLink
fullLink=r'http://www.junmeng.com%s'%imgLink
lct=time.strftime('%Y%m%d%H%M%S')
urllib.urlretrieve(fullLink,'%s\%s%d.jpg'%(savedir,lct,i))
#return data def reqImg2():
url=r'http://www.ik6.com/meinv/40569/index.html'
header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
savedir=r'C:\Users\hp\Desktop\results'
if not os.path.exists(savedir):
os.mkdir(savedir) for i in range(1,10):
if i==1:
tempurl=url
else:
tempurl='http://www.ik6.com/meinv/40569/index_%d.html'%i
print tempurl
#req=Request(tempurl,headers=header)
data=urllib.urlopen(tempurl).read()
page=BeautifulSoup(data)
imgsrc=page.find_all('center')[0].find_all('img')[0].get('lazysrc')
print imgsrc
lct=time.strftime('%Y%m%d%H%M%S')
urllib.urlretrieve(imgsrc,'%s\%s%d.jpg'%(savedir,lct,i)) def reqImg3():
url=r'http://www.ik6.com/meinv/40572/index.html'
header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
savedir=r'C:\Users\hp\Desktop\results'
if not os.path.exists(savedir):
os.mkdir(savedir)
for i in range(1,10):
if i==1:
tempurl=url
else:
tempurl='http://www.ik6.com/meinv/40572/index_%d.html'%i
print tempurl
#req=Request(tempurl,headers=header)
data=urllib.urlopen(tempurl).read()
page=BeautifulSoup(data)
imgsrc=page.find_all('center')[0].find_all('img')[0].get('lazysrc')
print imgsrc
lct=time.strftime('%Y%m%d%H%M%S')
urllib.urlretrieve(imgsrc,'%s\%s%d.jpg'%(savedir,lct,i)) def reqImg4(url,themecount,imgcount):
#url=r'http://www.ik6.com/meinv/40572/index.html'
header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
savedir=r'C:\Users\hp\Desktop\result0128'
if not os.path.exists(savedir):
os.mkdir(savedir)
newUrl=(url[:url.rfind('.htm')]+'_%d.html')
print newUrl
for i in range(1,imgcount+1):
if i==1:
tempurl=url
else:
tempurl=newUrl%i
print tempurl
try:
data=urllib.urlopen(tempurl).read()
if not data:
print 'no response,exit'
return
page=BeautifulSoup(data)
centers=page.find_all('center')
if len(centers)==0:
print 'response has no contents,exit'
return
else:
imgsrc=centers[0].find_all('img')[0].get('lazysrc')
print imgsrc
#lct=time.strftime('%Y%m%d%H%M%S')
#urllib.urlretrieve(imgsrc,'%s\%s%d.jpg'%(savedir,lct,i))
urllib.urlretrieve(imgsrc,'%s\%d_%d.jpg'%(savedir,themecount,i))
except Exception,e:
return

使用:

req('http://blog.csdn.net/suwei19870312/article/details/8148427')
req('http://www.taobao.com')
reqImg()
reqImg2()
reqImg3()
for i in range(1000):
count=11170+i
url=r'http://www.ik6.com/meinv/%d/index.html'%count
reqImg4(url,8)

Python配合BeautifulSoup读取网络图片并保存在本地的更多相关文章

  1. Java--多线程读取网络图片并保存在本地

    本例用到了多线程.时间函数.网络流.文件读写.正则表达式(在读取html内容response时,最好不要用正则表达式来抓捕html文本内容里的特征,因为服务器返回的多个页面的文本内容不一定使用相同的模 ...

  2. Python3 获取网络图片并且保存到本地

    Python3 获取网络图片并且保存到本地 import requests from bs4 import BeautifulSoup from urllib import request impor ...

  3. PHP获取网络图片并保存在本地目录

    PHP获取网络图片并保存在本地目录思路: 代码如下: function file_exists_S3($url) { $state = @file_get_contents($url,0,null,0 ...

  4. Python脚本连接数据库读取特定字段保存在文件中

    从Script表中取出Description字段作为文件名,并按协议将脚本归位相同的文件夹,取TestScript字段的内容写入文件 import MySQLdb import sys import ...

  5. python Image open读取网络图片本地显示 爬虫必备

    #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests from PIL import Image from io import Byte ...

  6. Java从网络读取图片并保存至本地

    package cn.test.net; import java.io.File; import java.io.FileOutputStream; import java.io.InputStrea ...

  7. JAVA获取网络图片并保存到本地(随机图片接口)

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...

  8. python 将验证码保存到本地 读取 写入

    #验证码 #读取验证码网址.打开本地路径.写入.输入验证码 downpicture = urllib.request.urlopen(SecretCodeUrl).read() local = ope ...

  9. 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台

    搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...

随机推荐

  1. WPF中资源引用方式汇总

    在WPF应用程序开发中,总是难以记住各种访问资源的方法,遂逐一记下. 先从资源是否编译到程序集分类 一.程序集资源 资源在编译的时候嵌入到程序集中.WPF中的XAML会被编译为BAML,图片等其他资源 ...

  2. 0505-NABCD模型、视频

    1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 NABCD模型: ...

  3. [转载]VS2012编译C语言scanf函数error的解决方法

    在VS 2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: error C4996: 'scanf': This function or variable may ...

  4. asp.net 学习

    1.维护数据库的完整性.一致性.你喜欢用触发器还是自写业务逻辑?为什么? 答:尽可能用约束(包括CHECK.主键.唯一键.外键.非空字段)实现,这种方式的效率最好:其次用触发器,这种方式可以保证无论何 ...

  5. Java设计模式-解释器模式(Interpreter)

    解释器模式是我们暂时的最后一讲,一般主要应用在OOP开发中的编译器的开发中,所以适用面比较窄. Context类是一个上下文环境类,Plus和Minus分别是用来计算的实现,代码如下: public ...

  6. Luncene 学习入门

    Lucene是apache组织的一个用java实现全文搜索引擎的开源项目. 其功能非常的强大,api也很简单.总得来说用Lucene来进行建立 和搜索和操作数据库是差不多的(有点像),Document ...

  7. 【UVALive 7334】Kernel Knights

    题 题意 有两个队的骑士1到n和n+1到2n,每个骑士只能互相攻击对手队的一个骑士.kernel的意思是在这个kernel里的骑士不会互相攻击,在kernel外的骑士被kernel里的骑士攻击. 现在 ...

  8. 【Matplotlib】 增加图例

    相关文档: Legend guide legend() command Legend API 控制图例入口 无参调用 legend() 会自动获取图例 handles 以及相关的 labels.其对应 ...

  9. 36.Android之多线程和handle更新UI学习

    android经常用到多线程更新UI,今天学习下. 首先布局比较简单: <?xml version="1.0" encoding="utf-8"?> ...

  10. 【BZOJ-2879】美食节 最小费用最大流 + 动态建图

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1366  Solved: 737[Submit][Status] ...