环境准备

1 python + requests + BeautifulSoup

页面准备

主页面:

http://www.netbian.com/dongman/

图片伪地址:

http://www.netbian.com/desk/22371.htm

图片真实地址:

http://img.netbian.com/file/2019/1221/36eb674ba0633d185da078804a3638e6.jpg

步骤

1 导入库

import requests
from bs4 import BeautifulSoup
import re

2 更改请求头

ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
# "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
# "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
# "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
# "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
# "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
# "Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"

3 获取主页面的内容

response = requests.get(url, headers={'User-Agent': ua})
html = response.text
soup = BeautifulSoup(html, 'html.parser')

4 我们要的是main里的list中的li标签中的a标签的href,而不是a标签里的img标签的src,若时获取img里的地址其大小为 800*450

list = soup.find(name='div', attrs='list')
for li in list.find_all('li'):
# print(img.attrs['src'])
for a in li.children:
if a.name == 'a':
src = 'http://www.netbian.com' + a.attrs['href']

5 截取连接里的数字作为图片的名称(这里可以自己想怎么弄就怎么弄)

n = re.search(r'\d+', a.attrs['href'])[0]    # 这里是\d+,而不是\d{5},是为了避免万一只出现4个数字,则会报错

6 到达真实图片地址

res = requests.get(src, headers={'User-Agent': ua})
s = BeautifulSoup(res.text, 'html.parser')
p = s.find(name='p')
# print(p)
img = p.img.attrs['src']
# print(img)
# 判断地址是否为空
if not img:
continue

7 下载

with requests.get(img, headers={'User-Agent': ua}) as resp:
# print(resp.status_code)
resp.raise_for_status()
resp.encoding = res.apparent_encoding
# 将图片内容写入
with open('E://paper//{}.jpg'.format(n), 'wb') as f:
f.write(resp.content)
f.close()

8 若要下载所有的图片

# 页数循环
for i in range(1, 139):
if i == 1:
url = 'http://www.netbian.com/dongman/index.htm'
else:
url = 'http://www.netbian.com/dongman/index_{}.htm'.format(i)
# print(url)

9 结果



注:

若会Xpath的话,用Xpath会比BeautifulSoup要简单点,我自己是懒得改过去了。

Python Download Image (python + requests + BeautifulSoup)的更多相关文章

  1. Python爬虫学习三------requests+BeautifulSoup爬取简单网页

    第一次第一次用MarkDown来写博客,先试试效果吧! 昨天2018俄罗斯世界杯拉开了大幕,作为一个伪球迷,当然也得为世界杯做出一点贡献啦. 于是今天就编写了一个爬虫程序将腾讯新闻下世界杯专题的相关新 ...

  2. 一个超实用的python爬虫功能使用 requests BeautifulSoup

    一个简单的数据爬取的示例 import os,re import requests import random import time from bs4 import BeautifulSoup us ...

  3. Python 爬虫—— requests BeautifulSoup

    本文记录下用来爬虫主要使用的两个库.第一个是requests,用这个库能很方便的下载网页,不用标准库里面各种urllib:第二个BeautifulSoup用来解析网页,不然自己用正则的话很烦. req ...

  4. python库:bs4,BeautifulSoup库、Requests库

    Beautiful Soup https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ Beautiful Soup 4.2.0 文档 htt ...

  5. 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)

    本篇文章是使用python抓取数据的第一篇,使用requests+BeautifulSoup的方法对页面进行抓取和数据提取.通过使用requests库对链家网二手房列表页进行抓取,通过Beautifu ...

  6. 【Python】在Pycharm中安装爬虫库requests , BeautifulSoup , lxml 的解决方法

    BeautifulSoup在学习Python过程中可能需要用到一些爬虫库 例如:requests BeautifulSoup和lxml库 前面的两个库,用Pychram都可以通过 File--> ...

  7. Python使用urllib,urllib3,requests库+beautifulsoup爬取网页

    Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...

  8. python 爬虫(一) requests+BeautifulSoup 爬取简单网页代码示例

    以前搞偷偷摸摸的事,不对,是搞爬虫都是用urllib,不过真的是很麻烦,下面就使用requests + BeautifulSoup 爬爬简单的网页. 详细介绍都在代码中注释了,大家可以参阅. # -* ...

  9. [python] 网络数据采集 操作清单 BeautifulSoup、Selenium、Tesseract、CSV等

    Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesseract.CSV等 Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesse ...

随机推荐

  1. Jmeter和nmon shell命令

    jmeter: sh jmeter -n -t /data/LPPZ/scripts/oauth.jmx -l /data/LPPZ/log/log.jtl nmon: ./nmon_linux_x8 ...

  2. formValidation单个输入框值改变时校验

     $("#tv_form").data("formValidation").updateStatus("pay.vcAmount", &qu ...

  3. [ DLPytorch ] 注意力机制&机器翻译

    MachineTranslation 实现过程 rstrip():删除 string 字符串末尾的指定字符(默认为空格). 语法:str.rstrip([chars]) 参数:chars -- 指定删 ...

  4. 「JSOI2014」序列维护

    「JSOI2014」序列维护 传送门 其实这题就是luogu的模板线段树2,之所以要发题解就是因为学到了一种比较NB的 \(\text{update}\) 的方式.(参见这题) 我们可以把修改操作统一 ...

  5. python 之并发编程更新版进程池与进程池比较与回调函数

    一.更新版进程池与进程池比较 from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor import os, tim ...

  6. java8新特性1:lambda表达式和函数式接口

    1.lambda的介绍: 1.1.为什么java语言需要引入lambda表达式? java语言诞生于1995年,历史时间已经相对较长了.在其后的各种新型编程语言中,都有着lambda表达式的内容,并且 ...

  7. python邮箱发送

    普通发送邮件 使用email模块和stmplib模块,内容比较固定,配好了即可实现,代码如下 一.普通邮箱发送 # -*- coding:utf-8-*- import smtplib from em ...

  8. 操作系统OS - 重装Windows7卡在completing installation

    1. shift + f10 2. cd oobe 3. Msoobe

  9. Preparing for the interview of FLAG and USDA

    7,Dynamic Programming 1,Unique Paths A robot is located at the top-left corner of a m x n grid (mark ...

  10. Servlet部署项目和项目起别名

    一.部署项目: ① 单机MyEclipse导航栏下方Deploy MyEclipse J2EE Project to Server... ②单机Add,选择Service,点击Ok 二.给项目起别名: ...