吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 10889|回复: 23
收起左侧

[Python 转载] Python3 - 爬取美图录

  [复制链接]
好想吃掉你 发表于 2019-3-22 11:11
本帖最后由 好想吃掉你 于 2019-3-27 15:53 编辑

[Python] 纯文本查看 复制代码
import requests
from bs4 import BeautifulSoup
import os
import chardet
from chardet import detect
import re


headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    'Accept-Encoding': 'gzip',
    "Referer": "https://www.meitulu.com/"
}   #头部文件
path="jpg" #jpg为保存目录,可以随意更改
os.chdir(path)

url = 'https://www.meitulu.com/t/qingdouke/'  #青豆客


#'https://www.meitulu.com/t/youwuguan/'  尤物馆
#'https://www.meitulu.com/t/girlt/'  果团网 , 'https://www.meitulu.com/t/mistar/', 魅妍社
#'https://www.meitulu.com/t/1088/', 星颜社 'https://www.meitulu.com/t/ishow/', iSHOW爱秀
#'https://www.meitulu.com/t/huayan/',花の颜 'https://www.meitulu.com/t/xingleyuan/',星乐园
#'https://www.meitulu.com/t/tukmo/', 'https://www.meitulu.com/t/aiss/',
#'https://www.meitulu.com/t/miitao/', 'https://www.meitulu.com/t/uxing/',
#'https://www.meitulu.com/t/taste/',  'https://www.meitulu.com/t/micat/',
#'https://www.meitulu.com/t/candy/', 'https://www.meitulu.com/t/yunvlang/',
#'https://www.meitulu.com/t/youmihui/', 'https://www.meitulu.com/t/1113/',
#'https://www.meitulu.com/t/1209/', 'https://www.meitulu.com/t/imiss/',
#'https://www.meitulu.com/t/yingsihui-wings/', https://www.meitulu.com/t/dianannan/ 嗲囡囡
#url可更改为上面随意一个
#美图录的随意一个目录url复制即可运行

response1 = requests.get(url, headers=headers, allow_redirects=False) 

e = chardet.detect(response1.content)['encoding']
response1.encoding = e

html_soup1 = BeautifulSoup(response1.text, 'lxml')
all_a = html_soup1.find('ul', class_='img').find_all('li') #
count=0
for l in all_a:
    count = count + 1
    a = l.a
    #print(a)


    if (a.find('img') == None):
        continue
    else:

        href = a["href"]
        print(href)

        href2=href.replace('.html', '_')
        response2 = requests.get(href, headers=headers)

        e = chardet.detect(response2.content)['encoding']
        response2.encoding = e

        html_soup2 = BeautifulSoup(response2.text, 'lxml')
        name = html_soup2.find('title').text.replace('/', '-')
        os.mkdir(name)
        print(name)
        img1 = html_soup2.find("div", {"class": "content"}).find_all("img")
        page = html_soup2.find("div", {"id": "pages"}).find_all('a')[-2]
        max_page = page.get_text()


        for pic in img1:
            os.chdir(name)
            count = count + 1
            pic1 = pic['src']
            # print(pic1)
            pic2 = requests.get(pic1, headers=headers)
            f = open(name+str(count) + '.jpg', 'ab') 
            f.write(pic2.content) 
            f.close()
            os.chdir("/Users/w2ng/untitled/venv/jpg")

        for i in range(2, int(max_page) + 1):
            os.chdir(name)
            href3 = href2 + str(i) + '.html'
            response3 = requests.get(href3, headers=headers)

            html_soup3 = BeautifulSoup(response3.text, 'lxml')
            img1 = html_soup3.find("div", {"class": "content"}).find_all("img")
            # print(href3)
            for pic in img1:
                count = count + 1

                pic1 = pic['src']
                # print(pic1)
                pic2 = requests.get(pic1, headers=headers)
                f = open(name+str(count) + '.jpg', 'ab') 
                f.write(pic2.content) 
                f.close()
            os.chdir("/Users/w2ng/untitled/venv/jpg") #获取自己的工作目录,将/Users/w2ng/untitled/venv 改为os.getcwd()的结果就ok


print("完成!")



测试图:
QQ20190327-155224@2x.png

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
吾爱妹子涛 + 1 + 1 不能白嫖。。
Wldwait + 1 + 1 我要装8个肾才够用

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

92monkey 发表于 2019-11-18 22:35
为啥我的是这个呢?
D:\Users\Administrator\Anaconda3\envs\pycharm\python.exe E:/pycharm/spider.py
  File "E:/pycharm/spider.py", line 7
     
    ^
SyntaxError: invalid character in identifier

Process finished with exit code 1
micen 发表于 2019-12-10 20:18
    os.chdir(name)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: '[QingDouKe青豆客] 甜美妹妹魏扭扭 - 校服写真娇羞露骨惹人怜[50P]_美图录'
95君 发表于 2019-3-22 13:40 来自手机
 楼主| 好想吃掉你 发表于 2019-3-27 15:55
已更新运行结果图
互动联想 发表于 2019-4-18 18:12
敢问大神如何调用你的代码呢,需要使用什么软件加载代码下载呢?
wangfeilong0317 发表于 2019-4-19 14:01
谢谢楼主的分享,相比于爬虫程序,我更感兴趣的是楼主放出来的网站链接
das3641 发表于 2019-4-22 17:00
既然有这么多url,为什么不做一个遍历或者接收一下input值来选择呢
cjwff 发表于 2019-4-22 22:31
谢谢分享带字动态图片
yiranww 发表于 2019-5-6 08:32
下载了,正在用,谢谢分享。
陌上雨下 发表于 2019-5-7 14:56
用不来哭哭哭
fish.1437 发表于 2019-5-13 14:56
感觉好厉害的样子,牛
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 03:09

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表