Web scraping 下载整个网页并使用urllib.request将其另存为html文件

Web scraping 下载整个网页并使用urllib.request将其另存为html文件,web-scraping,webpage,Web Scraping,Webpage,我可以使用这些代码保存多个网页;然而,我不能看到一个正确的网站视图后,他们保存为html。例如,表格中的文本出现滑动,图像无法显示。 我需要下载整个页面,就像我们在任何web浏览器中保存为一样,这样我才能看到正确的视图 import urllib.request url= 'https://asd.com/asdID=' for i in range(1, 5): print(' --> ID:', i) newurl = url + str(i) f =

我可以使用这些代码保存多个网页;然而,我不能看到一个正确的网站视图后,他们保存为html。例如,表格中的文本出现滑动,图像无法显示。 我需要下载整个页面,就像我们在任何web浏览器中保存为一样,这样我才能看到正确的视图

import urllib.request

url= 'https://asd.com/asdID='
for i in range(1, 5):
    print('     --> ID:', i)
    newurl = url + str(i)
    f = open(str(i)+'.html', 'w')
    page = urllib.request.urlopen(newurl)
    pagetext = str(page.read())
    f.write(pagetext)
    f.close()

您可以使用selenium来下载完整的网站 只需运行以下代码

from selenium import webdriver
#Download the chrome driver from the link below and specify the path of chromedriver
#https://chromedriver.storage.googleapis.com/index.html?path=2.40/
chromedriver = 'C:/python36/chromedriver.exe'
url= 'https://asd.com/asdID='
for i in range(1, 5):
    browser = webdriver.Chrome(chromedriver)
    browser.get(url + str(i))
    data = browser.page_source
    with open("webpage%s.html" %(str(i)), "w+") as f:
        f.write(data)
更新

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import ahk

firefox = FirefoxBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")
from selenium import webdriver

driver = web.Firefox(firefox_binary=firefox)
driver.get("http://www.yahoo.com")
ahk.start()
ahk.ready()
ahk.execute("Send,^s")
ahk.execute("WinWaitActive, Save As,,2")
ahk.execute("WinActivate, Save As")
ahk.execute("Send, C:\\path\\to\\file.htm")
ahk.execute("Send, {Enter}")

现在您将获得所有内容

当您获得一个网页时,您只需获得该网页的HTML代码即可。要下载图像、脚本等资产,样式表等。您需要提取HTML代码中的相应链接并单独下载。如果您遇到任何问题,请发表意见。如何将所有这些HTML文档合并到一个HTML文档中?将所有HTML文档合并到一个HTML文档中有何意义?如果将所有HTML文档合并到一个HTML文档中,则无法打开相应的网站实际上,我想在一个文档中查看所有信息。然后,我会将此html文档转换为docx格式。”ahk'在python 3中不可用。另一种方法是什么?“web.Firefox”应该是“webdriver.Firefox”吗?是的,应该是webdriver。要将所有文档合并为1,您需要将open(“webpage%s.html”%(str(i)),“w+”)更改为f:f。将(数据)写入open(“webpage.html”,“a”)as f:f写入(数据)