【问题标题】:Request a html file请求一个 html 文件
【发布时间】:2012-11-09 21:28:23
【问题描述】:

这个link 让我从数据库中获取一个随机项目。但是,我想使用 Python 自动检索项目。这是我的代码:

import sys
from urllib.parse import urlencode
from urllib.request import urlopen

# parameters
data = {}
data["query"] = "reviewd:yes+AND+organism:9606"
data["random"] = "yes"

url_values = urlencode(data)
url = "http://www.uniprot.org/uniprot/"
full_url = url + '?' + url_values
data = urlopen(full_url)
out = open("1.html", 'w')
out.write(str(data.read()))

但是,我无法获得所需的页面。有人知道我的代码有什么问题吗?我正在使用 Python 3.x。

【问题讨论】:

    标签: python http python-3.x


    【解决方案1】:

    你有几个问题:

    1. reviewd 拼写错误,应该是 reviewed
    2. 基本网址需要以/uniprot/结尾
    3. 您需要在查询字符串中使用空格而不是 +

    这就是它的样子:

    import sys
    from urllib.parse import urlencode
    from urllib.request import urlopen
    
    # parameters
    data = {}
    data["query"] = "reviewed:yes AND organism:9606"
    data["random"] = "yes"
    
    url_values = urlencode(data)
    url = "http://www.uniprot.org/uniprot/"
    full_url = url + '?' + url_values
    data = urlopen(full_url)
    out = open("1.html", 'w')
    out.write(str(data.read()))
    

    这会产生以下网址:

    http://www.uniprot.org/uniprot/?query=reviewed%3Ayes+AND+organism%3A9606&random=yes
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2011-11-13
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 2015-03-17
      相关资源
      最近更新 更多