栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

利用python提取excel中网页链接并下载对应网页

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用python提取excel中网页链接并下载对应网页

1.提取各个表链接

2.下载对应的网页并保存到对应的文件夹

小白,不保证准确性

import xlrd
import re
from jieba import xrange
from selenium import webdriver
import os


wb = xlrd.open_workbook('xxxx.xlsx')

p = wb.sheet_names()
obj = re.compile(r"https:/.*?d+")

for y in p:
    print(y)
    base_dir = os.path.dirname(__file__)

    if os.path.exists(base_dir + '/' + y):
        print('exist')
    else:
        os.mkdir(base_dir + '/' + y)  # 创建
    sh = wb.sheet_by_name(y)
    sheet_data = []
    for rownum in xrange(sh.nrows):
        sheet_data.append((sh.row_values(rownum)))
        found_list = []
        rows_to_be_saved = []
        for i in sheet_data:
            for cell in i:
                ret = obj.finditer(str(cell))
                for it in ret:
                    # print(it.group())
                    # found_list.append(it.group())

                    filename = os.path.join(base_dir + '/' + y.strip(), re.search(r"d+", it.group()).group() + '.mhtml')
                    if os.access(filename,os.F_OK):
                        print('Given file path is exist.')
                    else:
                        driver = webdriver.Chrome()
                        driver.get(it.group())
                    # 1. 执行 Chome 开发工具命令,得到mhtml内容
                        res = driver.execute_cdp_cmd('Page.captureSnapshot', {})

                    # 2. 写入文件

                        with open(filename, 'w', newline='') as f:
                            f.write(res['data'])
                        driver.quit()
                # result = obj.findall(str(cell))
                # print(result)
                # if cell.value == "string1" or i[2] == "string2" or i[2] == "string3" or i[2] == "string4" or i[2] == "string5":

            # else:

            # rows_to_be_saved.append(i)

print("over")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/468617.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号