import requests import re import os from bs4 import BeautifulSoup from scrapy import Spider from PIL import Image import io def GetBasicInfo(url): res = requests.get(url, headers=headers) res.encoding = 'utf-8' soup = BeautifulSoup(res.text, 'lxml') tmp = soup.find(attrs={'class': 'mhlistbody'}) chapters = tmp.ul.contents chapters.reverse() return chapters def GetRealUrls(mh_info): imgs = [] comic_size = re.findall(r'comic_size:"(.*?)"', mh_info)[0] base_url = 'https://mhpic.jumanhua.com/comic/{}.jpg%s.webp' % comic_size num_img = int(re.findall(r'totalimg:(\d+)', mh_info)[0]) pageid = int(re.findall(r'pageid:(\d+)', mh_info)[0]) imgpath = re.findall(r'imgpath:"(.*?)"', mh_info)[0] start = 0 while True: idx = imgpath.find('\\', start) if idx == -1: break imgpath = imgpath[:idx] + imgpath[idx+1:] start = idx + 1 for i in range(num_img): realpath = str() for s in imgpath: realpath += chr(ord(s) - pageid % 10) url = base_url.format(realpath + str(i+1)) imgs.append([url, str(i+1)+'.jpg']) return imgs def DownloadChapter(savepath, url): if not os.path.exists(savepath): os.mkdir(savepath) res = requests.get(url, headers=headers) res.encoding = 'utf-8' mh_info = re.findall(r'mh_info={(.*?)}', res.text)[0] img_urls = GetRealUrls(mh_info) for img_url in img_urls: img_content = requests.get(img_url[0]).content filename = os.path.join(savepath, img_url[1]) img = Image.open(io.BytesIO(img_content)) img.save(filename) if __name__ == '__main__': url = 'https://www.manhuatai.com/yaoshenji/' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36'} savepath = url.split('/')[-2] Spider(url,savepath)
时间: 2024-04-07 08:30:38 浏览: 105
这段代码是一个简单的Python脚本,用于下载指定漫画网站的漫画图片。它使用了`requests`库来发送HTTP请求,`re`库用于正则表达式匹配,`os`库用于处理文件路径,`BeautifulSoup`库用于解析HTML,`PIL`库用于处理图片,`io`库用于读取图片内容。
具体来说,脚本中的`GetBasicInfo`函数用于获取漫画的章节信息,`GetRealUrls`函数用于获取漫画章节中每页图片的真实URL,`DownloadChapter`函数用于下载指定章节的漫画图片。
在脚本的主程序中,设置了要下载的漫画网址和请求头信息,然后调用`Spider`函数来下载漫画。
需要注意的是,该脚本使用了第三方库和一些特定的网站结构,如果要使用该脚本下载其他网站的漫画,可能需要进行适当的修改。
希望以上解答能够帮到您!如果还有其他问题,请随时提问。
相关问题
import requests from bs4 import BeautifulSoup import pymysql import random from selenium import webdriver from selenium.webdriver import ChromeOptions import re import time import requests
这段代码有一个语法错误,应该在 import 语句之间加上换行符。正确的写法如下:
```
import requests
from bs4 import BeautifulSoup
import pymysql
import random
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import re
import time
import requests
```
import requests from bs4 import BeautifulSoup import re import docx from docx.oxml.ns import qn
这段代码缺少必要的语法结构。请将这些语句放置在一个函数或方法中,例如:
```python
import requests
from bs4 import BeautifulSoup
import re
import docx
from docx.oxml.ns import qn
def my_function():
# 在这里编写代码
```
请注意,这只是一个示例,您需要根据您的实际需求来编写函数或方法。在您的函数或方法中,您可以使用上述导入的库和模块来完成您的任务。
阅读全文
相关推荐














