Python配合BeautifulSoup读取网络图片并保存在本地
本例为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读取网络图片并保存在本地的更多相关文章
- Java--多线程读取网络图片并保存在本地
本例用到了多线程.时间函数.网络流.文件读写.正则表达式(在读取html内容response时,最好不要用正则表达式来抓捕html文本内容里的特征,因为服务器返回的多个页面的文本内容不一定使用相同的模 ...
- Python3 获取网络图片并且保存到本地
Python3 获取网络图片并且保存到本地 import requests from bs4 import BeautifulSoup from urllib import request impor ...
- PHP获取网络图片并保存在本地目录
PHP获取网络图片并保存在本地目录思路: 代码如下: function file_exists_S3($url) { $state = @file_get_contents($url,0,null,0 ...
- Python脚本连接数据库读取特定字段保存在文件中
从Script表中取出Description字段作为文件名,并按协议将脚本归位相同的文件夹,取TestScript字段的内容写入文件 import MySQLdb import sys import ...
- python Image open读取网络图片本地显示 爬虫必备
#!/usr/bin/python3 # -*- coding: utf-8 -*- import requests from PIL import Image from io import Byte ...
- Java从网络读取图片并保存至本地
package cn.test.net; import java.io.File; import java.io.FileOutputStream; import java.io.InputStrea ...
- JAVA获取网络图片并保存到本地(随机图片接口)
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...
- python 将验证码保存到本地 读取 写入
#验证码 #读取验证码网址.打开本地路径.写入.输入验证码 downpicture = urllib.request.urlopen(SecretCodeUrl).read() local = ope ...
- 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台
搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...
随机推荐
- 阿里百川IIMSDK-- 加好友,获取好友
加好友, 其实就是跟发消息一样 获取好友列表 同步好友列表 SDK内部默认会在每次登陆成功后与服务端同步好友列表,开发者可以通过设置disableAutoRequestAllContacts来禁用这个 ...
- go语言的模板,text/template包
go语言的模板,text/template包 定义 模板就是将一组文本嵌入另一组文本里 传入string--最简单的替换 package main import ( "os" &q ...
- zoj3882 博弈
我理解错题目意思,稀里糊涂A了.其实就是先手必胜. #include<stdio.h> int main() { int n; while(scanf("%d",&am ...
- C 文件读写2
feof() int feof(FILE *stream); 在执行读文件操作时,如果遇到文件尾,则函数返回逻辑真(1):否则,则返回逻辑假(0). feof()函数同时适用于ASCII码文件和二进 ...
- Android intent传递list或对象
方法一: 如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用 Java代码 intent.putStringArrayListExtra ...
- win10 1607 安装密钥 GVLK
Core=YTMG3-N6DKC-DKB77-7M9GH-8HVX7 Professional=VK7JG-NPHTM-C97JM-9MPGT-3V66T Enterprise=XGVPP-NMH47 ...
- Linux强制访问控制机制模块分析之mls_type.h
2.4.2 mls_type.h 2.4.2.1文件描述 对于mls_type.h文件,其完整文件名为security/selinux/ss/mls_types.h,该文件定义了MLS策略使用的类型. ...
- IOS基础之 (七) 分类Category
一 Category 分类:Category(类目,类别) (OC有) 命名:原来的类+类别名(原来的类名自动生成,只要写后面的类别名,一般以模块名为名.比如原来类 Person,新建分类 Ct,新建 ...
- JDK,JRE,JVM区别与联系(ZZ)
http://www.cnblogs.com/hencehong/p/3252166.html 我们开发的实际情况是:我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JD ...
- ECSHOP后台商品列表显示商品缩略图
ECSHOP后台商品列表显示商品缩略图 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-06 ecshop 后台商品列表显示商品缩略图,大楷步凑如下: ...