python学习之BeautifulSoup模块爬图
BeautifulSoup模块爬图学习HTML文本解析标签定位
网上教程多是爬mzitu,此网站反爬限制多了。随意找了个网址,解析速度有些慢。
脚本流程:首页获取总页数-->拼接每页URL-->获取每页中所有主题URL-->遍历图片源URL下载,保存
#python3
#coding:utf-8_
#_author: Jack
#_date: 2020/3/28 from bs4 import BeautifulSoup
import requests,os,sys,time DIR_PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.append(DIR_PATH) HEADER = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:74.0) Gecko/20100101 Firefox/74.0',
} def create_dir(file_path):
'''
:param file_path: images_directory
:return:
'''
if not os.path.exists(file_path):
os.makedirs(file_path)
print('Creatr directory:',file_path)
os.chdir(file_path) # cd .. def save_data(src,dir_name,file_name):
'''
:param src: images url
:param sum: directory name
:param file_name: image name
:return:
'''
file_path = os.path.join(DIR_PATH,'images',str(dir_name)) #directory path
image_path = os.path.join(file_path,file_name) #images path
create_dir(file_path) if not os.path.isfile(image_path):
req = requests.get(src,headers=HEADER)
with open(image_path, 'wb') as f_save:
f_save.write(req.content)
print('Download successful:',file_name)
f_save.flush()
else:
print('File already exists! Pass') def request_to_url(url,header):
'''
:param url: page_url
:param head: request.header
:return: respond.text
'''
res = requests.get(url,headers=header)
return res.text def soup(url,header):
'''
:param url:
:param header:
:return: HTML_Tag
'''
return BeautifulSoup(request_to_url(url,header),'html.parser') def action(url):
'''
Download a count of 100 images and create a new folder
:param url: URL
:return:
'''
download_count = 0
dir_name =100
try:
page_tag = soup(url,HEADER).find('div',class_='pg').find_all('a')
max_page = int(page_tag[-2].text.split(' ')[-1]) for i in range(1,max_page+1): #find page
page_url = os.path.join(url,'forum.php?order=&fid=0&page=%d'%i)
#time.sleep(1)
page_all_theme_list = soup(page_url,HEADER).find('div',class_='kind_show')
theme_list = page_all_theme_list.find_all('div', class_='photo_thumb kind_left') for i in theme_list: #find theme
theme = i.find('div', class_='title').find('a')
#title = theme.string
img_url = theme.get('href')
print("Ready download: %s" % theme.string,img_url)
# time.sleep(1)
img_page_tag = soup(img_url,HEADER).find('td',class_='t_f').find_all('img') for i in img_page_tag: #find image
try:
img_src = i.get('src')
if download_count %100 == 0:
dir_name +=100
save_data(img_src,dir_name,img_src.split('/')[-1])
download_count += 1
print('Download successful: %d' %download_count) except Exception as e:
print('Img_tag & Save_data Error:',e)
continue except Exception as e:
print('The trunk Error:',e) if __name__ == '__main__':
print('Run.....')
URL = 'http://www.lesb.cc/'
action(URL)
print('Perform !')
python学习之BeautifulSoup模块爬图的更多相关文章
- Python学习 Part4:模块
Python学习 Part4:模块 1. 模块是将定义保存在一个文件中的方法,然后在脚本中或解释器的交互实例中使用.模块中的定义可以被导入到其他模块或者main模块. 模块就是一个包含Python定义 ...
- python学习之argparse模块
python学习之argparse模块 一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行 ...
- Python学习day19-常用模块之re模块
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python学习day18-常用模块之NumPy
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- Python爬虫使用lxml模块爬取豆瓣读书排行榜并分析
上次使用了BeautifulSoup库爬取电影排行榜,爬取相对来说有点麻烦,爬取的速度也较慢.本次使用的lxml库,我个人是最喜欢的,爬取的语法很简单,爬取速度也快. 本次爬取的豆瓣书籍排行榜的首页地 ...
- 雨痕 的《Python学习笔记》--附脑图(转)
原文:http://www.pythoner.com/148.html 近日,在某微博上看到有人推荐了 雨痕 的<Python学习笔记>,从github上下载下来看了下,确实很不错. 注意 ...
- Python学习笔记-常用模块
1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...
- python学习之random模块
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- Python 爬虫三 beautifulsoup模块
beautifulsoup模块 BeautifulSoup模块 BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查 ...
随机推荐
- 分析Android中View的工作流程
在分析View的工作流程时,需要先分析一个很重要的类,MeasureSpec.这个类在View的测量(Measure)过程中会用到. MeasureSpec MeasureSpec是View的静态内部 ...
- Neural Turing Machine - 神经图灵机
Neural Turing Machine - 神经图灵机 论文原文地址: http://arxiv.org/pdf/1410.5401.pdf 一般的神经网络不具有记忆功能,输出的结果只基于当前的输 ...
- java基础进阶篇(二)_Arraylist ------【java源码栈】
前言 ArrayList 在开发中用到的频率很高,其中原生态提供的方法有一些很好用的重载版本,其中有的坑该跳得跳啊. 一.ArrayList的6种初始化方法1.构造方法 参数为空2.构造方法 参数为L ...
- java基础进阶篇(四)_HashMap------【java源码栈】
目录 一.前言 二.特点和常见问题 二.接口定义 三.初始化构造函数 四.HashMap内部结构 五.HashMap的存储分析 六.HashMap的读取分析 七.常用方法 八.HashMap 的jav ...
- tomcat服务器和http协议笔试题
tomcat与web程序结构与Http协议与HttpUrlConnection 考查的知识点:tomcat服务器相关信息 1.下面关于tomcat服务器描述正确的是() (难度A) A. tomcat ...
- 数据结构 1 线性表详解 链表、 栈 、 队列 结合JAVA 详解
前言 其实在学习数据结构之前,我也是从来都没了解过这门课,但是随着工作的慢慢深入,之前学习的东西实在是不够用,并且太皮毛了.太浅,只是懂得一些浅层的,我知道这个东西怎么用,但是要优化.或者是解析,就不 ...
- Kali虚拟机的扩容经历
Kali虚拟机的扩容经历 0x01 起因 更新了一下软件包,竟然提示我空间不足. 升级了 687 个软件包,新安装了 82 个软件包,要卸载 0 个软件包,有 8 个软件包未被升级. 需要下载 1,5 ...
- H5多列布局
多列布局 基本概念 1.多列布局类似报纸或杂志中的排版方式,上要用以控制大篇幅文本. 2.跨列属性可以控制横跨列的数量 /*列数*/ -webkit-column-count: 3; /*分割线*/ ...
- docker部署tensorflow serving以及模型替换
Using TensorFlow Serving with Docker 1.Ubuntu16.04下安装docker ce 1-1:卸载旧版本的docker sudo apt-get remove ...
- Django进行数据迁移时,报错:(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1")
进行数据迁移时: 第一步: 命令:python manage.py makemigrations 在对应的应用里面的migrations文件夹中产生了一个0001_initial.py文件 第二步:执 ...