【问题标题】:Python Request HTTP Page and Use The Source InformationPython 请求 HTTP 页面并使用源信息
【发布时间】:2013-05-14 01:10:22
【问题描述】:

我希望使用 python 请求一个网页并将源代码转换为字符串(我的页面只有文本,例如 Year2012,没有 html 或其他代码,所以如果你查看源代码,你会看到 Year2012,这就是我需要的)

例如,我在 Python 中有我想要的这个 C# 代码:

string WebClient = new WebClient().DownloadString(WebPage);

如果我创建 myscript.php 并输入 Year2012 string WebClient will be = "Year2012" 如果我​​稍后更改它并且我的程序检查网页,它将是我更改的内容。我想要同样的东西在 windows 的 python 中你知道怎么做吗?

谢谢!

【问题讨论】:

    标签: python python-2.7 python-2.6


    【解决方案1】:

    我建议你使用包requests。这是一个可能满足你需要的代码片段:

    import requests
    r = requests.get('https://github.com/timeline.json')
    

    编辑

    或者你可以使用python中的默认包:urllib2,一个简单的例子:

    import urllib2
    f = urllib2.urlopen('http://www.python.org/')
    print f.read(100)
    

    输出:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <?xml-stylesheet href="./css/ht2html
    

    【讨论】:

    • @prehistoicpenguin 谢谢!如何在 Windows 上安装软件包?没找到:(
    • @Mark 这里是安装页面:docs.python-requests.org/en/latest/user/install
    • @Mark 按照这个提示安装 pip:stackoverflow.com/questions/4750806/…
    • @Mark 然后用 pip 安装请求。
    • 谢谢,我现在用 pip for windows 安装它,请求是否有任何依赖关系,或者它可以在任何 PC 上运行吗?有没有办法在没有外部软件包的情况下实现这一点?
    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多