惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
U
Unit 42
Vercel News
Vercel News
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
C
Check Point Blog
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
博客园_首页
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
Last Week in AI
Last Week in AI
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
V
Visual Studio Blog
小众软件
小众软件

噜啦 - 爬虫

爬虫爬取图片 - 噜啦 使用爬虫爬取新闻网站标题 - 噜啦 使用爬虫爬取新闻网站标题 - 噜啦 结合正则表达式爬取网页 - 噜啦 结合正则表达式爬取网页 - 噜啦
爬虫爬取图片 - 噜啦
博主: 噜啦 · 2019-10-09 · via 噜啦 - 爬虫

爬虫爬取图片

  • 发布时间:
  • 2490 次浏览
  • 1479字数
  • 分类: Python笔记本
  1. 首页
  2. 正文  

代码

from bs4 import BeautifulSoup
import requests
import os
import shutil

headers = {
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
}

def download_jpg(imgge_url, image_localpath):
    response = requests.get(imgge_url, stream=True)
    if response.status_code == 200:
        with open(image_localpath, 'wb') as f:
            response.decode_content = True
            shutil.copyfileobj(response.raw, f)

def craw(url):
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'lxml')
    for div in soup.find_all('div', class_='group'):
        for img in div.find_all('img'):
            imgurl = img['src']
            dir = os.path.abspath('./download')
            filename = os.path.basename(imgurl)
            imgpath = os.path.join(dir, filename)
            print('开始下载 %s' % imgurl)
            download_jpg(imgurl, imgpath)


for i in range(1, 10, 1):
    url = 'http://xxxxxx.com/plugin.php?id=group&page=' + str(i)
    print(url)
    print('第 %s 页' %i)
    craw(url)

运行

7PiZ22V1Gu.jpg

营养有点不足,溜了溜了

赞赏作者

如果觉得我的文章对你有用,请随意赞赏

爬虫爬取图片

 •