Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中将网页数据保存到变量_Python_Python 3.x_Web_Rss - Fatal编程技术网

如何在Python中将网页数据保存到变量

如何在Python中将网页数据保存到变量,python,python-3.x,web,rss,Python,Python 3.x,Web,Rss,我已经四处寻找了一段时间,想找到答案,但不幸的是,今天运气不好 我正在尝试获取此网页的内容并将其保存到变量中 如果你对我的意思感到困惑,请在评论中提出一个问题,因为就个人而言,我不知道如何表达这个意思 请不要让我失望,我知道你能行;) 您需要安装请求。希望您知道pip,否则请仔细阅读并安装它 pip安装请求 然后在代码中: import requests url = "http://rivalregions.com/rss/all" req = requests.get(url) if r

我已经四处寻找了一段时间,想找到答案,但不幸的是,今天运气不好

我正在尝试获取此网页的内容并将其保存到变量中

如果你对我的意思感到困惑,请在评论中提出一个问题,因为就个人而言,我不知道如何表达这个意思


请不要让我失望,我知道你能行;)

您需要安装请求。希望您知道pip,否则请仔细阅读并安装它

pip安装请求

然后在代码中:

import requests

url = "http://rivalregions.com/rss/all"

req = requests.get(url)

if req.status_code in [200]:
    html = req.text
else:
    print 'Could not retrieve: %s, err: %s - status code: %s' % (url, req.text, req.status_code)
    html = None

您不需要安装请求就可以在python3中使用它。 下面的代码是使用python 3.6.10测试的

import urllib.request

def print_some_url():
    try:
        with urllib.request.urlopen('http://www.python.org/') as f:
           a_variable = f.read().decode('utf-8')
           print(a_variable)
    except urllib.error.URLError as e:
       print(e.reason)

print_some_url()   

我没有得到你需要的。。。如果您只想转储站点,可以将其卷曲:
curlhttp://rivalregions.com/rss/all >终端的站点
会将所有站点存储在一个名为站点的文件中。这只会给出无效的语法。您运行哪个操作系统?在windows中,您必须手动下载并安装它。在Linux中,根据您的发行版,您可能也需要先安装它。此时此刻,我正在使用kali Linux。但我通常使用Windows10。
apt安装curl
,然后
curlhttp://rivalregions.com/rss/all >文件