【问题标题】:How to read remote page content using Python如何使用 Python 读取远程页面内容
【发布时间】:2017-07-24 06:30:52
【问题描述】:

我需要使用 python 读取远程文件内容,但在这里我面临一些挑战。我的代码如下:

import subprocess

path = 'http://securityxploded.com/remote-file-inclusion.php'
subprocess.Popen(["rsync", host-ip+path],stdout=subprocess.PIPE)
for line in ssh.stdout:
    line  

在这里我收到错误NameError: name 'host' is not defined。我不知道host-ip 值应该是什么,因为我正在使用终端(python sub.py)运行我的 Python 文件。这里需要读取http://securityxploded.com/remote-file-inclusion.php远程文件的内容。

【问题讨论】:

标签: python python-2.7 subprocess


【解决方案1】:

您需要 urllib 库。此外,您正在使用您不使用的参数。 试试这样的:

import urllib.request

fp = urllib.request.urlopen("http://www.stackoverflow.com")
mybytes = fp.read()

mystr = mybytes.decode("utf8")
fp.close()
print(mystr)

注意:这是针对 python 3 的

对于 python 2.7 使用这个:

import urllib

fp = urllib.urlopen("http://www.stackoverflow.com")
myfile = fp.read()
print myfile

【讨论】:

  • 我使用的是 python 2.7。
【解决方案2】:

如果你想通过 http 读取远程内容。

requests 或 urllib2 都是不错的选择。

对于 Python2,使用请求。

import requests
resp = requests.get('http://example.com/')
print resp.text

会起作用的。

【讨论】:

    猜你喜欢
    • 2010-11-21
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2013-05-03
    相关资源
    最近更新 更多