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模块爬图的更多相关文章

  1. Python学习 Part4:模块

    Python学习 Part4:模块 1. 模块是将定义保存在一个文件中的方法,然后在脚本中或解释器的交互实例中使用.模块中的定义可以被导入到其他模块或者main模块. 模块就是一个包含Python定义 ...

  2. python学习之argparse模块

    python学习之argparse模块 一.简介: argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块.argparse模块的作用是用于解析命令行 ...

  3. Python学习day19-常用模块之re模块

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. Python学习day18-常用模块之NumPy

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  5. Python爬虫使用lxml模块爬取豆瓣读书排行榜并分析

    上次使用了BeautifulSoup库爬取电影排行榜,爬取相对来说有点麻烦,爬取的速度也较慢.本次使用的lxml库,我个人是最喜欢的,爬取的语法很简单,爬取速度也快. 本次爬取的豆瓣书籍排行榜的首页地 ...

  6. 雨痕 的《Python学习笔记》--附脑图(转)

    原文:http://www.pythoner.com/148.html 近日,在某微博上看到有人推荐了 雨痕 的<Python学习笔记>,从github上下载下来看了下,确实很不错. 注意 ...

  7. Python学习笔记-常用模块

    1.python模块 如果你退出 Python 解释器并重新进入,你做的任何定义(变量和方法)都会丢失.因此,如果你想要编写一些更大的程序,为准备解释器输入使用一个文本编辑器会更好,并以那个文件替代作 ...

  8. python学习之random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  9. Python 爬虫三 beautifulsoup模块

    beautifulsoup模块 BeautifulSoup模块 BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查 ...

随机推荐

  1. 码海拾遗:Linux常用命令(一)

    一.Linux系统安装 系统安装可以分两类:实体机安装Linux,虚拟机(常用虚拟机软件有两种:VMware和VirtualBox)安装Linux. 安装过程网上有很多教程,这里就不赘述了. 二.常用 ...

  2. Redis(2)——跳跃表

    一.跳跃表简介 跳跃表(skiplist)是一种随机化的数据结构,由 William Pugh 在论文<Skip lists: a probabilistic alternative to ba ...

  3. STL标准库中的容器

    容器:顾名思义,我的理解就是把同一种数据类型括起来,作为一捆.如vector<int> ,vector就是个容器,里面全是一个个的int型数据. 容器包括三大块: 顺序型容器: (1)ve ...

  4. Ajax&Json案例

    案例: * 校验用户名是否存在 1. 服务器响应的数据,在客户端使用时,要想当做json数据格式使用.有两种解决方案: 1. $.get(type):将最后一个参数type指定为"json& ...

  5. sql -- 利用order by 排名作弊

    表结构: 需求: 方法1:union ,,) order by user_total desc ) a union (,,) order by user_total desc ) b) 方法2:直接在 ...

  6. 记一次crontab执行和日志生成问题

    一.crontab未执行 crontab里面设置定时任务如下: 1 19 * * * /usr/bin/python3 /home/nola/a.py > /home/nola/logs/a_l ...

  7. 关于在layui中的table checkbox 默认选中设置

    一.layui版本 layui-v2.4.5 二.设置table的checkbox默认选中 总共有两种方法: 方法1:在返回的json中设置LAY_CHECKED为true,页面上的checkbox就 ...

  8. git版本回退问题记录

    因为之前有个前端改了文件目录进行合并时候丢失掉些许代码,然后我在以前分支进行了代码层级的整理,项目如果想要启动还需还原回以前的版本,我进行了三次文件夹层级提交,所以我需要进行三次的版本回退. git命 ...

  9. 内网渗透之信息收集-Linux系统篇

    linux 系统信息 grep MenTotal /proc/meminfo #查看系统内存总量 cat /etc/issue #查看系统名称 cat /etc/lsb-release #查看系统名称 ...

  10. Asp.NET MvC EF实现分页

    打开Visual Studio 2017 选择 项目----->管理nuget包  其他版本也有 输入paged 下载安装 pagedList和pagedList.mvc 在model文件新建一 ...