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. STM32 一个初始化EXTI的例子

    23 May 2017 » Hardware 注:STM32F407VGT6 with STM32F4 DSP and standard peripherals library v1.8.0 外部中断 ...

  2. 强制迁移、合区 APP太强势伤害用户同时是否违法?

    APP太强势伤害用户同时是否违法?" title="强制迁移.合区 APP太强势伤害用户同时是否违法?"> 对于经常混迹在国内各大手游的玩家来说,"合区& ...

  3. Oracle中的列转行实现字段拼接用例

    文章目录 Oracle中的列转行实现字段拼接 场景 在SQL使用过程中经常有这种需求:将某列字段拼接成in('XX','XX','XX','XX','XX','XX' ...)做为查询条件. 实现 s ...

  4. Leetcode 943. Find the Shortest Superstring(DP)

    题目来源:https://leetcode.com/problems/find-the-shortest-superstring/description/ 标记难度:Hard 提交次数:3/4 代码效 ...

  5. java基础知识点补充---二维数组

    #java基础知识点补充---二维数组 首先定义一个二维数组 int[][] ns={ {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} }; 实现遍 ...

  6. JMeter-WebService接口的测试

    前言 JMeter3.2版本之后就没有SOAP/XML-RPC Request插件了,那么该如何进行webservice接口的测试呢? 今天我们来一起学习一下怎么在3.2以后版本的JMeter进行we ...

  7. OC和C++混编需要注意的问题

    文章首发于github.io 2018-12-17 21:01:55 方案一 1. .c文件的identify and type右边栏修改为Objective-C source 2. Built se ...

  8. JZOJ 1492. 烤饼干

    1492. 烤饼干 (Standard IO) Description NOIP烤饼干时两面都要烤,而且一次可以烤R(1<=R<=10)行C(1<=C<=10000)列个饼干, ...

  9. Vue请求第三方接口跨域最终解决办法!2020最终版!

    废话少说,再百度的近三个小时尝试了近10种方法无解后,终于皇天不负有心人! 这个vue axios 跨域问题被我解决了! 需求:请求客户端ip地址获取客户ip,再根据ip获取用户位置 工具:Vue,a ...

  10. 关于vue-cli3打包时遇到Cannot assign to read only property 'exports' of object '#<Object>'问题的解决方法。

    vue-cli3打包时遇到Cannot assign to read only property 'exports' of object '#<Object>'问题的解决方法. 大致是说, ...