本帖最后由 Ldfd 于 2020-8-19 23:03 编辑
先讲个笑话,程序员是怎么教孩子数数的,0,1,2,3(所以第0张就很人道)
talk is cheap, show me the code.
[Python] 纯文本查看 复制代码 import requests
from lxml import etree
import re
import os
import threading
def makefolder():
for name in NameList:
os.makedirs('./comic/' + name, exist_ok=True)
def getsrc(url, num, sem):
webpage = requests.get(url, headers=headers).text
srcre = re.compile('upload/files/[0-9]*/[0-9]*/[0-9]*.jpg')
src = srcre.findall(webpage)
src_new = []
for x in src:
x = f"https://img.yaoyaoliao.com/{x}"
src_new.append(x)
length = len(src_new)
for y in range(length):
picture = requests.get(src_new[y]).content
with open(f"./commic/{NameList[num]}/第{y}张.jpg", 'wb')as pic:
pic.write(picture)
sem.release()
print(f"{NameList[num]}已经爬完")
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 '
'Safari/537.36 '
}
Website = 'https://m.bnmanhua.com'
html = requests.get('https://m.bnmanhua.com/comic/1163.html', headers=headers).text
xml = etree.HTML(html)
NameList = xml.xpath('/html/body/div[4]/div[2]/ul/li/a/text()')
UrlList = xml.xpath('/html/body/div[4]/div[2]/ul/li/a/@href')
UrlList_new = []
for ii in UrlList:
ii = Website + ii
UrlList_new.append(ii)
makefolder()
sema = threading.Semaphore(10)
for lmqz in range(len(UrlList_new)):
sema.acquire()
t = threading.Thread(target=getsrc, args=(UrlList_new[lmqz], lmqz, sema))
t.start()
严格符合PEP8,保证加到pycharm里是绿色对号[doge]
还有就别说那个入口了,动态语言的优势必须发挥(懒地改了*1)
欢迎大佬帮我优化优化
感谢@香谢枫林 的线程控制,解决了我分组的困扰
参考地址:https://www.52pojie.cn/thread-1117105-1-1.html
[Asm] 纯文本查看 复制代码 html = requests.get('https://m.bnmanhua.com/comic/1163.html', headers=headers).text
修改此处的数字换页面
这个写了有一段时间了,网站最新有些变化
图片内容有的是/upload有的是/zhang
改一下正则就能用,再加个判断就能完整下载
(我爬得是早期作品所以很简单懒得改*2)
有人需要(不可能的)就打包修改一下。
再次欢迎各路大神修改 |