python爬取照片(失败)

  python爬取漫画(失败)

一:获取每一章的url网址以及名字:

import re
import urllib
from bs4 import BeautifulSoup
import urllib.request
import urllib.parse
import time

main_web="http://www.kuman55.com"

pic=[]  #储存照片
findlist=[]
strname=[]  #储存每章漫画的名字
addr=[]  #储存每个网站的地址
findTitle=re.compile(r'<a href="(.*)" rel="nofollow">(.*)<span>')
findSource=re.compile(r'<a href=".*')

def collect(web):
    response=urllib.request.urlopen(web)
    time.sleep(2000)
    bs=BeautifulSoup(response.read(),"html")
    tag=bs.find(attrs={'class':'view-win-list detail-list-select'})
    for item in tag.find_all(name='a',rel='nofollow'):
        # 储存地址:获取a标签下的href元素,注意获取到了整个标签,那么只需要用数组中加引号再加标签名字就可以获取到内容
        addr.append(str(main_web+item['href']))
        item=str(item)
        findlist.append(item)

    save_info()

def save_info():
    for item in findlist:
        #可以了,记得要和原先的格式要一样,储存名字
        item=re.sub(r'<a href=".*" rel="nofollow">',"",item)
        item=re.sub(r'<span>(P)</span></a>',"",item)
        strname.append(item)

    for i in len(addr):
        save_picture(strname[i],addr[i])

def save_picture(name,address):
    print("")




# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    collect("http://www.kuman55.com/mulu/15762/1-1.html")

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

二:储存漫画图片(这里失败了,因为该网站使用Ajax动态隐藏掉了图片div标签,而且使用Data URI加密,目前还没有解码)

import re
import urllib
import urllib.request
import time
import requests
#进行Data URI编码所用的包
from base64 import b64decode
from bs4 import BeautifulSoup

headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.68'
    }

findsrc=re.compile(r'src="https://p.pstatp.com/origin(.*)"')
img=[]
strings=[]

def save_picture(name,address):
    # 导入请求库l
    html=requests.get(address)
    print(html.text)
    response=urllib.request.Request(url=address,headers=headers,method="POST")
    time.sleep(2)
    res=urllib.request.urlopen(response)
    bs=BeautifulSoup(res.read(),"html")
    # 使用BeautifulSoup来获取对应的标签的属性值:使用点get,里面再加一个属性值的字符串就好了,ok
    strings=str(bs.find(name="img",attrs={"class":"comicimg"}).get('src'))
    print(strings)

    #解码bs64格式的Data URI
    '''
        1.pip导入base64的包,这个不用pip下载,pycharm自带
        2.将src的前面编码方式和后面的码分离出来,两者使用逗号相连,而且只有一个
        3.使用b64decode编码器编译,并储存到字符节数组data里面
        4.将其字符集写入文件当中,就会生成所需要的文件(为什么导出来的图片是白板??)
    '''
    head1,encode=strings.split(',',1)
    data=b64decode(encode)
    with open("image_src.png","wb") as f:
        f.write(data)
        f.close()

    '''另外一种方法:
        1.导入base64下面的decodestring包
        2.打开图片文件
        3.将码先进行编码,然后转换成字符串
    '''





if __name__ == '__main__':
    save_picture("照一","http://www.kuman55.com/15762/1171599.html")
原文地址:https://www.cnblogs.com/instead-everyone/p/13797050.html