目标,豆瓣读书,

下载页面书籍图片。

import urllib.request
import re #使用正则表达式 def getJpg(date):
jpgList = re.findall(r'(img src="http.+?.jpg")([\s\S]*?)(.+?.alt=".+?.")',date)
return jpgList def downLoad(jpgUrl,sTitle,n):
try:
urllib.request.urlretrieve(jpgUrl,\
'C:\\Users\\74172\\source\\repos\\Python\\spidertest1\\images\\book.douban\\%s.jpg' %sTitle)
except Exception as e:
print(e)
finally:
print('图片%s下载操作完成' % n) def getTitle(date):
titleList = re.findall(r'title=".">',date)
return titleList if __name__ == '__main__':
url = 'https://book.douban.com/'
res = urllib.request.urlopen(url)
date = res.read().decode('utf-8')
date_jpg = getJpg(date)
imageTitle = getTitle(date)
global n
n = 1
for jpginfo in date_jpg:
s = re.findall(r'http.+?.jpg',str(jpginfo))
print(n,'--- url -->',str(s)[2:-2])
sTitleInfo = re.findall(r'alt=".+?."',str(jpginfo))
sTitleL = re.findall(r'".+?."',str(sTitleInfo))
sTitle = str(sTitleL)[3:-3]
downLoad(s[0],sTitle,n)
n = n + 1

又做了点修改,并将书名写入txt文件中

import urllib.request
import re #使用正则表达式 def getJpg(html):
jpgList = re.findall(r'(img src="http.+?.jpg")([\s\S]*?)(.+?.alt=".+?.")',html)
jpgList = re.findall(r'http.+?.jpg',str(jpgList))
return jpgList def downLoad(jpgUrl,sTitle,n):
try:
urllib.request.urlretrieve(jpgUrl,\
'C:/Users/74172/source/repos/Python/spidertest1/images/book.douban/%s.jpg' %sTitle)
finally:
print('图片---%s----下载操作完成' % sTitle) def getTitle(html):
titleList = re.findall(r'(img src="http.+?.jpg")([\s\S]*?)(.+?.alt=".+?.")',html)
titleList = re.findall(r'alt=".+?."',str(titleList))
titleList = re.findall(r'".+?."',str(titleList))
return titleList def writeTxt(imageTitle):
try:
#目录建立txt文件
f = open((url[8:-5]+'.txt'),"a",encoding="utf-8")
#写入
f.write(imageTitle+'\n')
finally:
if f:
#关闭文件
f.close() if __name__ == '__main__':
url = 'https://book.douban.com/'
res = urllib.request.urlopen(url)
html = res.read().decode('utf-8')
urlJpgs = getJpg(html)
imageTitle = getTitle(html)
n = 0
for urlJpg in urlJpgs:
print(n,'--- url -->',urlJpg)
downLoad(urlJpg,imageTitle[n][1:-1],n)
writeTxt(imageTitle[n][1:-1])
n = n + 1

python3爬虫.3.下载网页图片的更多相关文章

  1. Python3简单爬虫抓取网页图片

    现在网上有很多python2写的爬虫抓取网页图片的实例,但不适用新手(新手都使用python3环境,不兼容python2), 所以我用Python3的语法写了一个简单抓取网页图片的实例,希望能够帮助到 ...

  2. node:爬虫爬取网页图片

    代码地址如下:http://www.demodashi.com/demo/13845.html 前言 周末自己在家闲着没事,刷着微信,玩着手机,发现自己的微信头像该换了,就去网上找了一下头像,看着图片 ...

  3. [记录][python]python爬虫,下载某图片网站的所有图集

    随笔仅用于学习交流,转载时请注明出处,http://www.cnblogs.com/CaDevil/p/5958770.html 该随笔是记录我的第一个python程序,一个爬去指定图片站点的所有图集 ...

  4. python3爬虫.4.下载煎蛋网妹子图

    开始我学习爬虫的目标 ----> 煎蛋网 通过设置User-Agent获取网页,发现本该是图片链接的地方被一个js函数代替了 于是全局搜索到该函数 function jandan_load_im ...

  5. python3爬虫爬取网页思路及常见问题(原创)

    学习爬虫有一段时间了,对遇到的一些问题进行一下总结. 爬虫流程可大致分为:请求网页(request),获取响应(response),解析(parse),保存(save). 下面分别说下这几个过程中可以 ...

  6. Python多线程爬虫爬取网页图片

    临近期末考试,但是根本不想复习!啊啊啊啊啊啊啊!!!! 于是做了一个爬虫,网址为 https://yande.re,网页图片为动漫美图(图片带点颜色........宅男福利 github项目地址为:h ...

  7. 小白学Python(7)——利用Requests下载网页图片、视频

    安装 Requests 如果安装了Requests就已经可用了,否则要安装 Requests,只要在你的CMD中运行这个简单命令即可: pip install requests requests使用 ...

  8. python图片爬虫 - 批量下载unsplash图片

    前言 unslpash绝对是找图的绝佳场所, 但是进网站等待图片加载真的令人捉急, 仿佛是一场拼RP的战争 然后就开始思考用爬虫帮我批量下载, 等下载完再挑选, 操作了一下不算很麻烦, 顺便也给大家提 ...

  9. python脚本工具-1 制作爬虫下载网页图片

    参考:http://www.cnblogs.com/fnng/p/3576154.html 本文参考虫师的博客“python实现简单爬虫功能”,整理分析后抓取其他站点的图片并下载保存在本地. 抓取图片 ...

随机推荐

  1. c 读取文本

    #include <stdio.h> #include <stdlib.h> #include <string.h> #define max 10 #define ...

  2. 点击除指定元素以外的任意地方隐藏js

    $(document).click(function () { $(".search_box").hide(); }); $(".resultUl").on(& ...

  3. 堆模板(pascal)洛谷P3378

    题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...

  4. 【数据库_Mysql】<foreach>标签在Mysql中的使用

    foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径访问,如item.age,item.info.details.具体说明:在list和数组中是其中的对象,在map中是val ...

  5. BZOJ5072 小A的树(树形dp)

    容易猜到能选择的黑点个数是一个连续区间.那么设f[i][j]为i子树内选j个点形成包含根的连通块,最多有几个黑点,g[i][j]为最少有几个黑点,暴力dp是O(n2)的,求出每个连通块大小对应的黑点数 ...

  6. lb route 相关的一些问题

    lb route 相关的一些问题 ========================== 查看系统平台和版本 > show hardware Platform: NetScaler Virtual ...

  7. BZOJ2716:[Violet 3]天使玩偶——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2716 样例输入 2 31 12 32 1 21 3 32 4 2 样例输出 1 2 ———————— ...

  8. skip-external-locking --mysql配置说明

    MySQL的配置文件my.cnf中默认存在一行skip-external-locking的参数,即“跳过外部锁定”.根据MySQL开发网站的官方解释,External-locking用于多进程条件下为 ...

  9. [ACM][2018南京预赛]Lpl and Energy-saving Lamps

    一.题面 样例输入: 5 4 3 10 5 2 7 10 5 1 4 8 7 2 3 6 4 7 样例输出: 4 0 1 1 3 6 5 1 5 1 2 0 3 2 4 4 3 6 5 1 二.思路 ...

  10. [POJ1094] Sorting It All Out

    link 题目大意 给出$m$个不等式关系,问可以从第几个开始确定所有之间的大小关系.若无解请输出是无法确定还是与已知矛盾. 试题分析 这题是真的是坑啊,尽然放在$floyd$传到闭包上面,还用二分, ...