如何使用Python读取远程页面内容

2024-10-01 09:16:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用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远程文件的内容。在


Tags: 文件pathipcomhttphost内容远程
2条回答

你需要urllib库。你也在使用你不使用的参数。 试试这样的方法:

import urllib.request

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

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

注意:这是针对python3的

对于python 2.7,请使用以下命令:

^{pr2}$

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

请求或urllib2都是不错的选择。在

对于Python2,使用请求。在

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

会有用的。在

相关问题 更多 >