Python wget 保存文件。如何获取变量中的数据

标签 python

我在Python中使用wget作为

import wget
from bs4 import BeautifulSoup
url = "https://www.facebook.com/hellomeets/events"

down = wget.download(url)
print down

并将 html 数据下载到文件中。但我想把它放在变量上。我是Python新手。任何帮助,将不胜感激。 提前致谢

最佳答案

您不需要使用wget将HTML下载到文件中然后读入,您可以直接获取HTML。这是使用 requests (我认为比 python urllibs 更好)

import requests
from bs4 import BeautifulSoup
url = "https://www.facebook.com/hellomeets/events"

html = requests.get(url).text
print html

这是一个使用 urllib2 中内置的 python 的示例:

import urllib2
from bs4 import BeautifulSoup
url = "https://www.facebook.com/hellomeets/events"

html = urllib2.urlopen(url).read()
print html

编辑

我知道您所说的直接从网站获取的 HTML 与从 wget 模块获取的 HTML 之间的区别是什么意思。以下是使用 wget 模块执行此操作的方法:

import wget
from bs4 import BeautifulSoup
url = "https://www.facebook.com/hellomeets/events"

down = wget.download(url)

f = open(down, 'r')
htmlText = "\n".join(f.readlines())
f.close()
print htmlText

关于Python wget 保存文件。如何获取变量中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873684/

相关文章:

python - ctype在代码中没有发现属性错误

python - Django:作为值列表查询一部分的相关模型值列表

python:对具有相同第一个元素的元组的元素进行分组

python - 为什么要在Python的虚拟环境中创建requirements.txt文件?

python - 继承Python中同名的类

python - unittest库如何确定是否运行装饰方法?

python - pytesseract 找不到指定的文件

python ,被杀了?

python - Python 中的组合学

Python + MySQL : Insert geojson polygon value to mysql sptial column