本例为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. Sencha Touch 手机移动开发框架 HTML5 项目压缩方案;

    Sencha Touch框架生成基本项目目录结构 Index.html/ App.js App.json /touch[sdk]/ /Sencha-touch.js /src Resources/ A ...

  2. Ibatis学习总结1--ibatis简介和SQL Maps

    最佳维护的一个项目使的是ibatis框架,在闲暇之余将手头的开发手册和平时开发的理解做一下总结,言归正传. 简介 使用 SQL Map,能够大大减少访问关系数据库的代码.SQL Map 使用简单的 X ...

  3. WordPress HOOK机制原理及代码分析

    WordPress强大的插件机制让我们可以自由扩展功能.网上对插件的使用以及开发方法都有大量资料可以查询. 今天我们就分析一下四个主要函数的代码,包括: add_action.do_action.ad ...

  4. Java设计模式-原型模式(Prototype)

    原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象.这就是选型模式的用意. 原型模式的结构 原型模式要求对象实现一个可以“克 ...

  5. make xconfig时,出现“Unable to find any QT installation"错误

    在make menuconfig的时候,提示是找不到libncurses,最后其实是找不到libncurses-dev,又因为debian系统装的是libncurses5,所以装了libncurses ...

  6. Activity启动模式 及 Intent Flags 与 栈 的关联分析

     http://blog.csdn.net/vipzjyno1/article/details/25463457    Android启动模式Flags栈Task   目录(?)[+] 什么是栈 栈 ...

  7. eclipse中使用git

    有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...

  8. error MSB4019: 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets”

    error MSB4019: 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\ ...

  9. MyEclipse代码提示快捷键和常用设置

    我使用的是MyEclipse 6.0版本,代码助手(content assist)的快捷键由 Alt + / 改成了 Ctrl + Space,恰好我的输入法快捷键也是 Ctrl + Space .造 ...

  10. SSN 社会安全号码

    SSN是 Social Security Number 的缩写,译为「社会安全号码」,为美国社会安全卡(Social Security Card)上的 9 位数字.SSN 原本目的是用于追踪个人的纳税 ...