(70)--爬取哦漫画图片并下载到相应文件夹

# 爬取哦漫画图片并下载到相应文件夹

from selenium import webdriver
import time
import requests
from bs4 import BeautifulSoup
import re
from urllib import request,parse
import os

# 1.获取漫画章节链接

phantom = webdriver.PhantomJS(executable_path=r'E:\Python\phantomjs-2.1.1-windows\bin\phantomjs.exe')

# 获取章节链接
def getSectionLink():
    base_url = 'http://www.omanhua.com/comic/4014/'
    response = requests.get(base_url)
    response.encoding = 'utf-8'
    html = response.text
    html = BeautifulSoup(html,'lxml')

    # 创建漫画文件夹
    # 获取漫画名称
    manga_name = html.select('div.main01_content h2')[0].text.strip('漫画简介:')
    manga_path = 'img/' + manga_name
    if not os.path.exists(manga_path):
        os.makedirs(manga_path)

    # 创建章节文件夹
    section_link = html.select('div.subBookList ul li a')
    section_link.reverse()
    for index,section in enumerate(section_link):
        section_name = section.text
        section_path = manga_path + '/' + str(index) + '-' + section_name
        if not os.path.exists(section_path):
            os.makedirs(section_path)

    # 获取章节链接
    link_list = html.select('div.subBookList ul li a')
    link_list.reverse()
    for index,link in enumerate(link_list):
        link_section = link['href']
        fullurl = 'http://www.omanhua.com' + link_section
        section_path = manga_path + '/' + str(index) + '-' + link.text
        print(section_path)
        getManga(fullurl,section_path)


def getManga(fullurl,section_path):
    print(fullurl)
    # 获取最大页数
    response = requests.get(fullurl)
    response.encoding = 'utf-8'
    html = response.text
    max_pat = re.compile('id="page".*?span>/(\d+)',re.S)


    # 获取章节链接
    res = max_pat.search(html)

    if res is not None:
        max_page = res.group(1)
        for i in range(1,int(max_page) + 1):
            page_fullurl = fullurl + 'index.html?p=' + str(i)

            getMangaPage(page_fullurl,section_path)
    else:
        print('最大页数获取失败')

# 下载漫画
def getMangaPage(fullurl,section_path):
    phantom.get(fullurl)
    time.sleep(0.1)
    html = phantom.page_source
    html = BeautifulSoup(html,'lxml')
    img_url = html.select('img#mangaFile')[0]['src']

    # 下载图片
    fname = img_url.split('/')[-1]
    res = img_url.split('/')
    to_code = res[-2]

    to_code = parse.urlencode({'':to_code}).strip('=')
    res[-2] = to_code


    img_url = '/'.join(res)
    img_url = img_url.replace('+',' ')
    response = requests.get(img_url)


    # 转码

    with open(section_path + '/' + fname,'wb') as f:
        f.write(response.content)

if __name__ == '__main__':
    getSectionLink()
    phantom.quit()

# 爬取结果如下:



兄弟连学python


Python学习交流、资源共享群:563626388 QQ


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用Python爬虫爬取图片的步骤: 1. 导入需要的库:requests、os、urllib。 2. 使用requests库发送请求,获取网页的HTML源代码。 3. 使用BeautifulSoup库解析HTML源代码,获取图片的URL地址。 4. 使用os库创建一个本地文件夹,用于存储下载的图片。 5. 使用urllib库下载图片,并保存在本地文件夹中。 6. 将上述步骤封装成函数,可用于批量下载图片。 下面是一个简单的代码示例: ``` import requests import os from bs4 import BeautifulSoup import urllib def download_images(url, folder_path): # 发送请求,获取HTML源代码 response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') # 查找所有的图片标签 img_tags = soup.findAll('img') # 创建本地文件夹 os.makedirs(folder_path, exist_ok=True) # 遍历所有图片标签,下载图片并保存到本地文件夹中 for img_tag in img_tags: img_url = img_tag.get('src') if img_url: img_path = os.path.join(folder_path, img_url.split('/')[-1]) urllib.request.urlretrieve(img_url, img_path) # 调用函数,下载图片 download_images('https://www.example.com', 'images') ``` 上述代码中,函数`download_images`接受两个参数:`url`表示要下载图片的网页URL地址,`folder_path`表示要保存下载图片的本地文件夹路径。函数使用requests库发送请求,获取HTML源代码,然后使用BeautifulSoup库解析HTML代码,查找所有的图片标签,遍历所有图片标签,下载图片并保存到本地文件夹中。最后调用`download_images`函数,传入相应的参数即可完成图片下载

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值