用Python爬取彼岸图网图片

 用Python爬取彼岸图网图片

*使用了  四个模块
import time
import requests
from lxml import etree
import os

没有的话自行百度安装。

#encoding = utf-8
import time
import requests
from lxml import etree
import os

# http://www.netbian.com/ 爬虫
if __name__ == '__main__':
    filePath = './保存图片'
    if not os.path.exists(filePath):
        os.mkdir(filePath)
    page_next = 'http://www.netbian.com/dongman/index.htm' #第一页
    header = { #UA伪装
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36 Edg/89.0.774.77"
    }
    for _ in range(1,133):
        page_text = requests.get(url=page_next, headers=header)
        page_text.encoding = 'gbk'
        tree = etree.HTML(page_text.text)
        li_list = tree.xpath('//div[@class="list"]//li')
        _next = tree.xpath('//div[@class="page"]/a[@class="prev"]/@href')
        if len(_next) == 1:
            page_next = 'http://www.netbian.com/' + _next[0]
        else:
            page_next = 'http://www.netbian.com/' + _next[1]
        for _ in li_list:
            time.sleep(0.3)
            href = _.xpath('./a/@href')
            if href != []:
                href = href[0]
            else :
                continue
            if href == 'https://pic.netbian.com/': #广告
                continue
            page_url = 'http://www.netbian.com/' + href
            title = _.xpath('./a/img/@alt')[0]
            spanIndex = title.find(' ',0,len(title)) #空格位置
            title = filePath + '/' + title[0:spanIndex] + '.jpg'
            img_page = requests.get(url=page_url, headers=header)
            _tree = etree.HTML(img_page.text)
            img_url = _tree.xpath('//div[@class="pic"]//a/img/@src')[0]
            try:
                img_file = requests.get(img_url, headers=header, stream=True)
                if img_file != None:
                    with open(title, 'wb') as f:
                        f.write(img_file.content)
                        print(title + '下载成功')
            except:
                print('异常咯,不用管')
    print('Over 全部下载完成')

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!可以使用Python的Requests库和BeautifulSoup库来实现爬取彼岸图网多页的功能,具体操作如下: 首先,需要用Requests库发送HTTP请求获取网页的HTML源码,可以定义一个函数来实现: ```python import requests def get_html(url): headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"} response = requests.get(url, headers=headers) return response.text ``` 这个函数会使用User-Agent头部伪装成浏览器,以防止被服务器识别为爬虫。 接下来,需要用BeautifulSoup库解析HTML源码并获取图片链接,可以定义一个函数来实现: ```python from bs4 import BeautifulSoup def get_img_urls(html): soup = BeautifulSoup(html, 'html.parser') img_urls = [] for img in soup.find_all('img', class_='pic-large'): img_urls.append("https://pic.netbian.com" + img['src']) return img_urls ``` 这个函数会使用find_all方法找到所有class为“pic-large”的<img>标签,并将其src属性加上前缀“https://pic.netbian.com”后添加到一个列表中。 最后,可以定义一个主函数来控制爬取多页的流程,具体代码如下: ```python def main(): for i in range(1, 11): url = "https://pic.netbian.com/index_%d.html" % i html = get_html(url) img_urls = get_img_urls(html) for img_url in img_urls: print(img_url) # 可以调用其他函数来完成保存图片等操作 ``` 这个函数会依次访问1到10页的网页,获取每页的HTML源码并解析出其中的图片链接,然后对每个图片链接执行一些操作,比如保存到本地或上传到云端。 注意:在实现爬虫的过程中需要注意法律法规和道德规范,不要用于非法用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值