如何保存“完整网页”不仅仅是使用Python的基本HTML




我使用以下代码使用 Python保存网页:
import urllib
import sys
from bs4 import BeautifulSoup

url = 'http://www.vodafone.de/privat/tarife/red-smartphone-tarife.html'
f = urllib.urlretrieve(url,'test.html')

问题:这个代码将html作为基本的html没有javascript,图像等.我想保存网页完整(就像我们在浏览器中有选项)

更新:
我现在使用以下代码来保存webapge的所有js / images / css文件,以便它可以保存为完整的网页,但是我的输出html仍然像基本的html一样被保存:

import pycurl
import StringIO

c = pycurl.Curl()
c.setopt(pycurl.URL,"http://www.vodafone.de/privat/tarife/red-smartphone-tarife.html")

b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION,b.write)
c.setopt(pycurl.FOLLOWLOCATION,1)
c.setopt(pycurl.MAXREDirs,5)
c.perform()
html = b.getvalue()
#print html
fh = open("file.html","w")
fh.write(html)
fh.close()

解决方法

尝试用 selenium模拟浏览器.此脚本将弹出网页的另存为对话框.您仍然必须弄清楚如何模拟按Enter键进行下载以开始,因为文件对话框不在硒的范围内(您的操作依赖于操作系统).
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

br = webdriver.Firefox()
br.get('http://www.google.com/')

save_me = ActionChains(br).key_down(Keys.CONTROL)\
         .key_down('s').key_up(Keys.CONTROL).key_up('s')
save_me.perform()

另外我认为以下@Amber建议抓住链接的资源可能会更简单,从而更好的解决方案.不过,我觉得使用硒是一个很好的起点,因为br.page_source会让你整个dom以及javascript生成的动态内容.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些