为了账号安全,请及时绑定邮箱和手机立即绑定

在 python 中,我有 100 个页面的链接并想将它们保存为 html

在 python 中,我有 100 个页面的链接并想将它们保存为 html

繁花不似锦 2023-04-18 17:29:50
里面有 100 页的链接(links.txt)这是我到目前为止的代码(它只保存一页)但是缺少保存所有 99 页的部分import requestsimport urllib.request, urllib.error, urllib.parse   with open('links.txt', 'r') as links:    for link in links:        response = urllib.request.urlopen(link)        webContent = response.read()        f = open('obo-t17800628-33.html', 'wb')        f.write(webContent)        f.close
查看完整描述

1 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

您需要在循环时为文件指定不同的名称:


import requests

import urllib.request, urllib.error, urllib.parse

   

with open('links.txt', 'r') as links:

    for idx, link in enumerate(links):

        response = urllib.request.urlopen(link)

        webContent = response.read()

        with open('obo-t17800628-33.html' + str(idx), 'wb') as fout:

            fout.write(webContent)

这将在每个文件名的末尾附加一个数字。


查看完整回答
反对 回复 2023-04-18
  • 1 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信