gpt4 book ai didi

python - 网页源代码的固有保存方式

转载 作者:太空狗 更新时间:2023-10-29 17:01:44 25 4
gpt4 key购买 nike

我已经阅读了很多关于网络抓取的答案,这些答案谈论的是 BeautifulSoup、Scrapy 等。执行网页抓取。

有没有一种方法可以实现与从网络浏览器保存页面源代码相同的效果?

也就是说,在 Python 中是否有一种方法可以将它指向一个网站并让它仅使用标准 Python 模块将页面的源代码保存到一个文本文件中?

这是我要去的地方:

import urllib

f = open('webpage.txt', 'w')
html = urllib.urlopen("http://www.somewebpage.com")

#somehow save the web page source

f.close()

我知道的不多 - 但正在寻找代码来实际提取页面的源代码,以便我可以编写它。我知道 urlopen 只是建立了联系。

也许有一个 readlines() 等效于读取网页的行?

最佳答案

你可以试试urllib2:

import urllib2

page = urllib2.urlopen('http://stackoverflow.com')

page_content = page.read()

with open('page_content.html', 'w') as fid:
fid.write(page_content)

关于python - 网页源代码的固有保存方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13332192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com