Python爬虫快看漫画

新手学python爬虫中,用这个网站练了一下手,代码还是有点乱,还需要优化,这是第一个版本,没有加超线程和错误调试

在win10下测试通过,不知道为啥放在在linux执行到filename那行出错,碎觉了不改了.

import requests
from bs4 import BeautifulSoup
import os
import re

headers={'Referer': 'http://www.kuaikanmanhua.com/',
         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',}

s=requests.session()
s.headers=headers

#基本设置
#保存根路径,默认在当前目录下
basepath='./'
#漫画集的首页,示例见下面的网址
url='http://www.kuaikanmanhua.com/web/topic/2047/'

def savejpg(url,path):
    global s
    filename=re.search('.*/(.*\.jpg)',url)[1]
    res=s.get(url)
    #res=requests(url,headers=headers)
    if res.status_code==200:
        print('保存图片'+filename+'到'+path)
        with open(path+filename,'wb') as f:
            f.write(res.content)
            f.close()

def get_imgs(url,path):
    global s
    html=s.get(url).text
    soup=BeautifulSoup(html,'html.parser')
    img_links=soup.select('.kklazy')
    for img_link in img_links:
        savejpg(img_link['data-kksrc'],path)


def parser_index(url):
    comic_img_info={}
    soup=BeautifulSoup(s.get(url).text,'html.parser')
    comic_name=soup.select('.comic-name')[0].text
    comic_titles=soup.findAll('a',attrs={'class':' article-img'})
    for titles in comic_titles:
        comic_img_info['name']=comic_name
        comic_img_info['title']=titles['title']
        comic_img_info['url']='http://www.kuaikanmanhua.com/'+titles['href']
        yield comic_img_info

def main(url=url,basepath=basepath):
    for item in parser_index(url):
        path=basepath+item['name']+'/'+item['title']+'/'
        if not os.path.exists(path):os.makedirs(path)
        get_imgs(item['url'],path)

if __name__=='__main()__':
    main()
  

捕获.PNG
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 11,324评论 0 10
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,741评论 25 709
  • 爬虫文章 in 简书程序员专题: like:128-Python 爬取落网音乐 like:127-【图文详解】py...
    喜欢吃栗子阅读 22,556评论 4 411
  • 线条的介绍及运用 线条是组成轮廓的基本要素,只需要拥有一支普通的笔,就能画出许多花样来。 通过对轮廓的应用,画出一...
    婧旭儿阅读 1,499评论 0 0
  • 纤纤翠竹小日中, 细细枫叶伴苍松。 渡月桥下东流水, 只恨岚山叶未红。
    落花逐水流阅读 1,405评论 0 0

友情链接更多精彩内容