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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 16969|回复: 73
收起左侧

[Python 转载] Python3-爬取妹子图

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

这里是第一页的爬虫如果想爬第几页就换url如果想全部爬下来的话,for循环就ok
以下是代码:
[Python] 纯文本查看 复制代码
from bs4 import BeautifulSoup
import requests
import os

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.mzitu.com/101553"
}   #头部文件
path="jpg" #jpg为保存目录,可随意更改
os.chdir(path)

url = 'https://www.mzitu.com'
response1 = requests.get(url, headers=headers)
html_soup1 = BeautifulSoup(response1.text, 'lxml')
all_url=html_soup1.find("ul",{"id":"pins"}).find_all("a")
count=1
for href in all_url:
    count=count+1
    if count%2!=0:
        href1=href['href']
        #print(href1)
        for href2 in href:
            response2 = requests.get(href1, headers=headers)
            html_Soup2 = BeautifulSoup(response2.text, 'lxml')
            pic_address= html_Soup2.find("div",{"class":"main-image"}).find("img")['src']
            #print(pic_address)
            next_address=html_Soup2.find_all("span")[9]
            max_adress=next_address.get_text()
            name = html_Soup2.find("div", {"class": "main-image"}).find("img")['alt']
            os.mkdir(name)
            os.chdir(name)
            for i  in range (1,int(max_adress)+1):
                #print(i)
                next_url = href1+'/'+str(i)
                response3 = requests.get(next_url, headers=headers)
                html_Soup3 = BeautifulSoup(response3.text, 'lxml')
                pic_address2 = html_Soup3.find("div", {"class": "main-image"}).find("img")['src']
                title = html_Soup3.find("h2")
                name1 = title.get_text()
                img = requests.get(pic_address2, headers=headers)
                f = open(name1 + '.jpg', 'ab')
                f.write(img.content)
                f.close()
            os.chdir("/Users/w2ng/untitled/venv/jpg")#获取自己的工作目录,将/Users/w2ng/untitled/venv 改为os.getcwd()的结果就ok


附上截图,运行了一下下


QQ20190327-124629@2x.png

免费评分

参与人数 5吾爱币 +3 热心值 +4 收起 理由
孤独色的夜 + 1 牛。。。。。。
树欲静 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
xcc1594 + 1 + 1 我很赞同!
a525 + 1 我很赞同!
swx5201314 + 1 + 1 学习了,谢谢大佬分享

查看全部评分

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

q597220017 发表于 2019-3-27 13:11
我好了 胸弟们
xw0224 发表于 2019-8-1 10:59
indian806 发表于 2019-8-1 09:50
非常感谢,那个问题按照您说的已经解决了现在问题是:
1.要么远程主机已关闭(估计是公司服务 ...

1,网站已经发觉你是爬虫了
2,提取规则错误或者网站更改了
3,没明白你的描述
xw0224 发表于 2019-8-1 13:33
indian806 发表于 2019-8-1 11:04
1.如果是提取规则错误应该如何解决,我目前用的是楼主的源码;
2."Referer": "https://www.****.com/101 ...

1、提取规则错误需要改源码
2、referer记录你从哪个url到达当前的url
XiaoYuGan0103 发表于 2019-8-1 14:31
阿伟死了
电话情思 发表于 2019-3-27 12:58
得需要装那个 环境软件吧
ytahdou 发表于 2019-3-27 13:02
爬虫是Python的长项。
GK218 发表于 2019-3-27 13:04
这爬的啥网站
kyle233 发表于 2019-3-27 13:13
感谢楼主分享
weliong 发表于 2019-3-27 13:24
看了下网站图片 只能说:万恶的 亮光马赛克啊
zmtc19870108 发表于 2019-3-27 13:26
成年男人的福利,谢谢分享。
zmtc19870108 发表于 2019-3-27 13:28
这代码怎么用???
yhcfsr 发表于 2019-3-27 13:36
一会试试再来评价
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-26 19:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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