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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1704|回复: 6
收起左侧

[Python 转载] Python+MySQL爬取有连续编号网页的内容和图片

[复制链接]
suny1925 发表于 2022-5-30 09:09
本帖最后由 suny1925 于 2022-5-30 09:12 编辑

初次发帖,多多关照。
[Python] 纯文本查看 复制代码
import os
import requests
import html
from bs4 import BeautifulSoup
import pymysql

# 打开数据库
db = pymysql.connect(host="localhost", user="root", password="root", db="szxx")

hd = {'user-agent': 'chrome/10'}


def download_all_html(id):
    try:
        url = 'https://*.com/html/' + str(id) + '.html' # 内容页网址,自己设置
        request = requests.get(url)
        return request.text
    except:
        # print('download page ' + id + ' error')
        return ''


def parse_single_html(id):
    try:
        # print("HTML:" + html)
        soup = BeautifulSoup(download_all_html(id), 'html.parser')
        divs = soup.find_all('div', {'class': 'site-list'})
        if not divs:
            return '';
        title = divs[0].find('h3', {'class': 'panel-title'}).text.strip()
        content = divs[0].find('div', {'id': 'frameContent'}).text.strip()
        imgs = divs[0].find_all('img')
        for img in imgs:
            path = img['src']
            tmp = path.replace(':', '')
            if path.find('?') > -1:
                filename = tmp[:tmp.index('?')].replace('https//*.com/uploadimages/', '') #图片地址解析
            else:
                filename = tmp
            filepath = os.path.dirname(os.path.realpath('e:/szxx/{}'.format(filename)))

            if not os.path.exists(filepath):
                os.makedirs(filepath, 0o777)

            try:
                response = requests.get(path)

                with open('e:/szxx/{}'.format(filename), 'wb') as f:
                    f.write(response.content)
            except:
                pass

        cur = db.cursor()
        sql_sel = "select count(*) from shijuan where id=" + str(id)
        cur.execute(sql_sel)
        results = cur.fetchall()

        content = html.escape(content)

        if results[0][0] > 0:
            sql = "update shijuan set title = '" + title + "', content='" + content + "' where id="+str(id)
        else:
            sql = "insert into shijuan(`id`,`title`,`content`) values (" + str(id) + ", '" + title + "', '" + content + "')"
            # print(sql)

        try:
            cur.execute(sql)
            # 提交
            db.commit()
            print('No.' + str(id) + ' download success')
        except Exception as e:
            # 错误回滚
            db.rollback()

    except Exception as e:
        print('**No.' + str(id) + ' download error')
        print(e.with_traceback())


for i in range(1, 10000, 1):
    parse_single_html(i)

db.close()

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

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

小木曾雪菜 发表于 2022-5-30 10:58
这种没必要用mysql,增加额外依赖,用sqlite足够了
cdsgg 发表于 2022-5-30 11:51
cdsgg 发表于 2022-5-30 11:56
建议mysql里面只存图片路径 或者图片链接 别存图片二进制  太大了 而且极其影响读取效率
seawaycao 发表于 2022-5-30 12:03
谢谢分享!收藏备用。
 楼主| suny1925 发表于 2022-6-4 16:38
cdsgg 发表于 2022-5-30 11:51
mysql存储图片 我记得会导致读取变慢好像

图片不是存在数据库,是存储在磁盘,数据库抓取的内容里有图片的URL,转换一下就可以了。
 楼主| suny1925 发表于 2022-6-4 16:40
小木曾雪菜 发表于 2022-5-30 10:58
这种没必要用mysql,增加额外依赖,用sqlite足够了

电脑中有现成的MySQL环境,用着顺手就直接用了。
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-5-7 14:59

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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