Python3 获取网络图片并且保存到本地
Python3 获取网络图片并且保存到本地
import requests
from bs4 import BeautifulSoup
from urllib import request
import sys
import re
import os def getNews(title,url,m):
Hostreferer = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
req = request.Request(url)
response = request.urlopen(req)
#过滤非utf-8的网页新闻
response = response.read().decode('utf-8',"ignore")
soup = BeautifulSoup(response,'lxml')
tag = soup.find('div',class_='article')
if tag == None:
return#获取文章内容
rep = re.compile("[\s+\.\!\/_,$%^*(+\"\']+|[+<>?、~*()]+")
title = rep.sub('',title)
title = title.replace(':',':')
filename = sys.path[0]+"/news/"+title+".txt"
with open(filename,'w',encoding='utf8') as file_object:
file_object.write("网址:"+url)
file_object.write("\n")
file_object.write(title)
file_object.write(tag.get_text()) i = 0
for image in tag.find_all('div','img_wrapper'):
title_img = title +str(i)
# 保存图片
#判断目录是否存在
if (os.path.exists(sys.path[0]+"/news/"+title)):
pass
else:
#不存在,则新建目录
os.mkdir(sys.path[0]+"/news/"+title)
os.chdir(sys.path[0]+"/news/"+title)
file_name = "http://news.***.com.cn/"+image.img.get('src').replace('//','')
html = requests.get(file_name, headers=Hostreferer)
# 图片不是文本文件,以二进制格式写入,所以是html.content
title_img = title_img +".jpg"
f = open(title_img, 'wb')
f.write(html.content)
f.close()
i+=1 print('成功爬取第', m,'个新闻',title)
return 0
Python3 获取网络图片并且保存到本地的更多相关文章
- PHP获取网络图片并保存在本地目录
PHP获取网络图片并保存在本地目录思路: 代码如下: function file_exists_S3($url) { $state = @file_get_contents($url,0,null,0 ...
- JAVA获取网络图片并保存到本地(随机图片接口)
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...
- php 获取远程图片保存到本地
php 获取远程图片保存到本地 使用两个函数 1.获取远程文件 2.把图片保存到本地 /** * 获取远程图片并把它保存到本地 * $url 是远程图片的完整URL地址,不能为空. */ functi ...
- Java--多线程读取网络图片并保存在本地
本例用到了多线程.时间函数.网络流.文件读写.正则表达式(在读取html内容response时,最好不要用正则表达式来抓捕html文本内容里的特征,因为服务器返回的多个页面的文本内容不一定使用相同的模 ...
- Python配合BeautifulSoup读取网络图片并保存在本地
本例为Python配合BeautifulSoup读取网络图片,并保存在本地. BeautifulSoup可代替正则表达式,更好地解析Html文本,获取其中的指定内容,如Tag.Property等 # ...
- pyhton 从web获取json数据 保存到本地然后再读取
从web中获取json数据直接进行处理总认为太慢.主要是从web中获取获取数据的过程有点慢. 所以就在想 假设先利用空暇时间把json数据获取并保存到本地,然后再从本地文件里读取和操作.应该就要快非常 ...
- Android 获取屏幕截图 和保存到本地的sd卡路径下
/** * 获取和保存当前屏幕的截图 */ private void GetandSaveCurrentImage() { //1.构建Bitmap WindowManager windowManag ...
- 从URL获取图片并保存到本地
/// <summary> /// HttpWebRequest Property /// </summary> /// <param name="fileNa ...
- JAVA 获取网页源代码保存到本地文件
package httpget; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundExce ...
随机推荐
- php intval的取值范围:与操作系统相关
php intval的取值范围:与操作系统相关,32位系统上为-2147483648到2147483647,64位系统上为-9223372036854775808到922337203685477580 ...
- Linq 多连接及 left join 实例 记录
var retList = from d in mbExList.Cast<MaterialBaseEx>().ToList() join c in umcList.Cast<Cla ...
- xcode工程编译错误:No architectures to compile for
问题 开发环境:xcode6,iPhone6模拟器 xcode工程编译错误:No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active ...
- Monkey and Banana---hdu1069(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意就是给你n种长方体每种类型不限制个数,然后我们把它们一个个堆起来,并且要满足下面的要比上面的 ...
- Pycharm模板添加默认信息
我们在Pycharm中使用到python3.x版本的解释器,完全没有问题可以正常使用,但是有的时候多少会出现使用python2的时候 我们明明都把代码和文字注释了,为什么使用的时候还会报错呢?? 报错 ...
- Spark SQL入门用法与原理分析
Spark SQL是为了让开发人员摆脱自己编写RDD等原生Spark代码而产生的,开发人员只需要写一句SQL语句或者调用API,就能生成(翻译成)对应的SparkJob代码并去执行,开发变得更简洁 注 ...
- [LeetCode] 127. Word Ladder _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [Leetcode] 49. Group Anagrams_Medium
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- window.open和window.showModalDialog
window.open window.open是打开一个新窗口 在window.open打开的窗口中刷新父页面 opener.location.reload(); 打开一个窗口格式:属性可以任意设置 ...
- linux命令:linux文件处理命令
命令格式 : 命令 [-选项] [参数] 例:ls -la /etc 说明:1)个别命令使用不遵循此格式,[]代表可选 2)当有多个选项时,可以写在一起 3)-a等于 --all,调用简化选项用 ...