|  | 
 
| 
#写于学习Python教程P57之后,未使用正则表达式
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  ##网址:http://jandan.net/ooxx
 ##代码未考虑非jpg格式图片,已知网站还有gif格式图片,有待改进
 
 
 复制代码
import urllib.request
import os
def open_url(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36')
    response = urllib.request.urlopen(url)
    html = response.read()
    return html
def find_imgs(url):
    html = open_url(url).decode('utf-8')
    pic_list = []
    a = html.find('查看原图')
    while a!=-1:
        c = a + 4
        a = a - 127
        b = html.find('.jpg', a) + 4
        pic_list.append('https://' + html[a:b])
        a = html.find('查看原图', c)
    return pic_list
def get_next_page(url):
    html = open_url(url).decode('utf-8')
    a = html.find('下一页') - 80
    a = html.find('//', a)
    b = html.find('comments', a)
    return 'https:' + html[a:b] + 'comments'
def sava_imgs(folder, pic_list):
    for each in pic_list:
        filename = each.split('/')[-1]
        with open(filename,'wb') as f:
            img = open_url(each)
            f.write(img)
def g_pic_dl(folder='妹子图',pages=10):
    pic_list = []
    url = "http://jandan.net/ooxx"
    os.mkdir(folder)
    os.chdir(folder)
    while pages > 0:
        pic_list=find_imgs(url)
        sava_imgs(folder, pic_list)
        url = get_next_page(url)
if __name__ == '__main__':
    g_pic_dl()
 | 
 |