Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 如何在Python中使用黑客新闻API?_Python 2.7_Hacker News - Fatal编程技术网

Python 2.7 如何在Python中使用黑客新闻API?

Python 2.7 如何在Python中使用黑客新闻API?,python-2.7,hacker-news,Python 2.7,Hacker News,黑客新闻发布了一个API,我如何在Python中使用它 我想得到所有的高层职位。我尝试使用urllib,但我认为我做得不对 这是我的密码: import urllib2 response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty') html = response.read() print response.read() 它只是打印空的 '' 我漏了一行,更新了代

黑客新闻发布了一个API,我如何在Python中使用它

我想得到所有的高层职位。我尝试使用
urllib
,但我认为我做得不对

这是我的密码:

import urllib2
response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
html = response.read()
print response.read()
它只是打印空的

''

我漏了一行,更新了代码

As@jornsharpe解释说
read()
只是一次操作。所以,如果您打印
html
,您将获得所有ID的列表。如果你浏览了这个列表,你必须再次提出每个请求以获得每个id的故事

首先,您必须将接收到的数据转换为python列表,并对它们进行检查

base_url =  'https://hacker-news.firebaseio.com/v0/item/{}.json?print=pretty'
top_story_ids = json.loads(html)
for story in top_story_ids:
    response = urllib2.urlopen(base_url.format(story))
    print response.read()
您可以使用它,而不是所有这些,它是黑客新闻API的Python包装。以下代码将获取顶级故事的所有ID:

from hackernews import HackerNews
hn = HackerNews()
top_story_ids = hn.top_stories()
# >>> top_story_ids
# [8432709, 8432616, 8433237, ...]
然后,您可以通过该循环打印所有内容,例如:

for story in top_story_ids:
   print hn.get_item(story)

免责声明:我写了
haxor

作为@jornsharpe,解释了
read()
只是一次操作。所以,如果您打印
html
,您将获得所有ID的列表。如果你浏览了这个列表,你必须再次提出每个请求以获得每个id的故事

首先,您必须将接收到的数据转换为python列表,并对它们进行检查

base_url =  'https://hacker-news.firebaseio.com/v0/item/{}.json?print=pretty'
top_story_ids = json.loads(html)
for story in top_story_ids:
    response = urllib2.urlopen(base_url.format(story))
    print response.read()
您可以使用它,而不是所有这些,它是黑客新闻API的Python包装。以下代码将获取顶级故事的所有ID:

from hackernews import HackerNews
hn = HackerNews()
top_story_ids = hn.top_stories()
# >>> top_story_ids
# [8432709, 8432616, 8433237, ...]
然后,您可以通过该循环打印所有内容,例如:

for story in top_story_ids:
   print hn.get_item(story)

免责声明:我写了
haxor

作为@jornsharpe,解释了
read()
只是一次操作。所以,如果您打印
html
,您将获得所有ID的列表。如果你浏览了这个列表,你必须再次提出每个请求以获得每个id的故事

首先,您必须将接收到的数据转换为python列表,并对它们进行检查

base_url =  'https://hacker-news.firebaseio.com/v0/item/{}.json?print=pretty'
top_story_ids = json.loads(html)
for story in top_story_ids:
    response = urllib2.urlopen(base_url.format(story))
    print response.read()
您可以使用它,而不是所有这些,它是黑客新闻API的Python包装。以下代码将获取顶级故事的所有ID:

from hackernews import HackerNews
hn = HackerNews()
top_story_ids = hn.top_stories()
# >>> top_story_ids
# [8432709, 8432616, 8433237, ...]
然后,您可以通过该循环打印所有内容,例如:

for story in top_story_ids:
   print hn.get_item(story)

免责声明:我写了
haxor

作为@jornsharpe,解释了
read()
只是一次操作。所以,如果您打印
html
,您将获得所有ID的列表。如果你浏览了这个列表,你必须再次提出每个请求以获得每个id的故事

首先,您必须将接收到的数据转换为python列表,并对它们进行检查

base_url =  'https://hacker-news.firebaseio.com/v0/item/{}.json?print=pretty'
top_story_ids = json.loads(html)
for story in top_story_ids:
    response = urllib2.urlopen(base_url.format(story))
    print response.read()
您可以使用它,而不是所有这些,它是黑客新闻API的Python包装。以下代码将获取顶级故事的所有ID:

from hackernews import HackerNews
hn = HackerNews()
top_story_ids = hn.top_stories()
# >>> top_story_ids
# [8432709, 8432616, 8433237, ...]
然后,您可以通过该循环打印所有内容,例如:

for story in top_story_ids:
   print hn.get_item(story)
免责声明:我写了
haxor

你应该

print html
而不是

print response.read()
为什么??因为
读取
是一次性操作;完成后,不能重复:

>>>import ullrib2
>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
>>> response.read()
'[ 8445087, 8444739, 8444603, 8443981, 8444976, 8443902, 8444252, 8444634, 8444931, 8444272, 8444025, 8441939, 8444510, 8444640, 8443830, 8445076, 8443470, 8444785, 8443028, 8444077, 8444832, 8443841, 8443467, 8443309, 8443187, 8443896, 8444971, 8443360, 8444601, 8443287, 8441095, 8441681, 8441055, 8442712, 8444909, 8443621, 8442596, 8443836, 8442266, 8443298, 8445122, 8443096, 8441699, 8442119, 8442965, 8440486, 8442093, 8443393, 8442067, 8444989, 8440985, 8444622, 8438728, 8442555, 8444880, 8442004, 8443185, 8444370, 8436210, 8437671, 8439641, 8443727, 8441702, 8436309, 8441041, 8437367, 8422087, 8441711, 8438063, 8444212, 8439408, 8442049, 8440989, 8439367, 8438515, 8437403, 8435278, 8442486, 8442730, 8428522, 8438904, 8443450, 8432703, 8430412, 8422928, 8443635, 8439267, 8440191, 8439560, 8437230, 8442556, 8439977, 8444140, 8441682, 8443776, 8441209, 8428632, 8441388, 8422599, 8439547 ]\n'
>>> response.read()
''
不过,在您的例子中,您已经将
read
中的字符串指定给了名称
html
,因此您仍然可以访问它


拥有故事ID后,您可以通过
“…/v0/item/{item number}.json?print=pretty”
访问每个故事ID:

>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/item/8445087.json?print=pretty')
>>> print response.read()
{
  "by" : "lalmachado",
  "id" : 8445087,
  "kids" : [ 8445205, 8445195, 8445173, 8445103 ],
  "score" : 21,
  "text" : "",
  "time" : 1413116430,
  "title" : "Show HN: Powerful ASCII art editor designed for the Mac",
  "type" : "story",
  "url" : "http://monodraw.helftone.com/"
}
继续之前,你应该通读一遍。这也值得一试。

你应该

print html
而不是

print response.read()
为什么??因为
读取
是一次性操作;完成后,不能重复:

>>>import ullrib2
>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
>>> response.read()
'[ 8445087, 8444739, 8444603, 8443981, 8444976, 8443902, 8444252, 8444634, 8444931, 8444272, 8444025, 8441939, 8444510, 8444640, 8443830, 8445076, 8443470, 8444785, 8443028, 8444077, 8444832, 8443841, 8443467, 8443309, 8443187, 8443896, 8444971, 8443360, 8444601, 8443287, 8441095, 8441681, 8441055, 8442712, 8444909, 8443621, 8442596, 8443836, 8442266, 8443298, 8445122, 8443096, 8441699, 8442119, 8442965, 8440486, 8442093, 8443393, 8442067, 8444989, 8440985, 8444622, 8438728, 8442555, 8444880, 8442004, 8443185, 8444370, 8436210, 8437671, 8439641, 8443727, 8441702, 8436309, 8441041, 8437367, 8422087, 8441711, 8438063, 8444212, 8439408, 8442049, 8440989, 8439367, 8438515, 8437403, 8435278, 8442486, 8442730, 8428522, 8438904, 8443450, 8432703, 8430412, 8422928, 8443635, 8439267, 8440191, 8439560, 8437230, 8442556, 8439977, 8444140, 8441682, 8443776, 8441209, 8428632, 8441388, 8422599, 8439547 ]\n'
>>> response.read()
''
不过,在您的例子中,您已经将
read
中的字符串指定给了名称
html
,因此您仍然可以访问它


拥有故事ID后,您可以通过
“…/v0/item/{item number}.json?print=pretty”
访问每个故事ID:

>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/item/8445087.json?print=pretty')
>>> print response.read()
{
  "by" : "lalmachado",
  "id" : 8445087,
  "kids" : [ 8445205, 8445195, 8445173, 8445103 ],
  "score" : 21,
  "text" : "",
  "time" : 1413116430,
  "title" : "Show HN: Powerful ASCII art editor designed for the Mac",
  "type" : "story",
  "url" : "http://monodraw.helftone.com/"
}
继续之前,你应该通读一遍。这也值得一试。

你应该

print html
而不是

print response.read()
为什么??因为
读取
是一次性操作;完成后,不能重复:

>>>import ullrib2
>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
>>> response.read()
'[ 8445087, 8444739, 8444603, 8443981, 8444976, 8443902, 8444252, 8444634, 8444931, 8444272, 8444025, 8441939, 8444510, 8444640, 8443830, 8445076, 8443470, 8444785, 8443028, 8444077, 8444832, 8443841, 8443467, 8443309, 8443187, 8443896, 8444971, 8443360, 8444601, 8443287, 8441095, 8441681, 8441055, 8442712, 8444909, 8443621, 8442596, 8443836, 8442266, 8443298, 8445122, 8443096, 8441699, 8442119, 8442965, 8440486, 8442093, 8443393, 8442067, 8444989, 8440985, 8444622, 8438728, 8442555, 8444880, 8442004, 8443185, 8444370, 8436210, 8437671, 8439641, 8443727, 8441702, 8436309, 8441041, 8437367, 8422087, 8441711, 8438063, 8444212, 8439408, 8442049, 8440989, 8439367, 8438515, 8437403, 8435278, 8442486, 8442730, 8428522, 8438904, 8443450, 8432703, 8430412, 8422928, 8443635, 8439267, 8440191, 8439560, 8437230, 8442556, 8439977, 8444140, 8441682, 8443776, 8441209, 8428632, 8441388, 8422599, 8439547 ]\n'
>>> response.read()
''
不过,在您的例子中,您已经将
read
中的字符串指定给了名称
html
,因此您仍然可以访问它


拥有故事ID后,您可以通过
“…/v0/item/{item number}.json?print=pretty”
访问每个故事ID:

>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/item/8445087.json?print=pretty')
>>> print response.read()
{
  "by" : "lalmachado",
  "id" : 8445087,
  "kids" : [ 8445205, 8445195, 8445173, 8445103 ],
  "score" : 21,
  "text" : "",
  "time" : 1413116430,
  "title" : "Show HN: Powerful ASCII art editor designed for the Mac",
  "type" : "story",
  "url" : "http://monodraw.helftone.com/"
}
继续之前,你应该通读一遍。这也值得一试。

你应该

print html
而不是

print response.read()
为什么??因为
读取
是一次性操作;完成后,不能重复:

>>>import ullrib2
>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty')
>>> response.read()
'[ 8445087, 8444739, 8444603, 8443981, 8444976, 8443902, 8444252, 8444634, 8444931, 8444272, 8444025, 8441939, 8444510, 8444640, 8443830, 8445076, 8443470, 8444785, 8443028, 8444077, 8444832, 8443841, 8443467, 8443309, 8443187, 8443896, 8444971, 8443360, 8444601, 8443287, 8441095, 8441681, 8441055, 8442712, 8444909, 8443621, 8442596, 8443836, 8442266, 8443298, 8445122, 8443096, 8441699, 8442119, 8442965, 8440486, 8442093, 8443393, 8442067, 8444989, 8440985, 8444622, 8438728, 8442555, 8444880, 8442004, 8443185, 8444370, 8436210, 8437671, 8439641, 8443727, 8441702, 8436309, 8441041, 8437367, 8422087, 8441711, 8438063, 8444212, 8439408, 8442049, 8440989, 8439367, 8438515, 8437403, 8435278, 8442486, 8442730, 8428522, 8438904, 8443450, 8432703, 8430412, 8422928, 8443635, 8439267, 8440191, 8439560, 8437230, 8442556, 8439977, 8444140, 8441682, 8443776, 8441209, 8428632, 8441388, 8422599, 8439547 ]\n'
>>> response.read()
''
不过,在您的例子中,您已经将
read
中的字符串指定给了名称
html
,因此您仍然可以访问它


拥有故事ID后,您可以通过
“…/v0/item/{item number}.json?print=pretty”
访问每个故事ID:

>>> response = urllib2.urlopen('https://hacker-news.firebaseio.com/v0/item/8445087.json?print=pretty')
>>> print response.read()
{
  "by" : "lalmachado",
  "id" : 8445087,
  "kids" : [ 8445205, 8445195, 8445173, 8445103 ],
  "score" : 21,
  "text" : "",
  "time" : 1413116430,
  "title" : "Show HN: Powerful ASCII art editor designed for the Mac",
  "type" : "story",
  "url" : "http://monodraw.helftone.com/"
}


继续之前,你应该通读一遍。这也值得一试。

无法复制-我得到
”[8444739,8445087,8444603,8443981,8444976,8443902,…,8420274]\n'
我错过了一行,我更新了我的问题。我如何将这个数字列表转换为帖子?检查我编辑的答案。无法复制-我得到
'[8444739,8445087,8444603,8443981,8444976,8443902,…,8420274]\n'
我漏了一行,我更新了我的问题。我如何将这个数字列表转换为帖子?检查我编辑的答案。无法复制-我得到
“[8444739,8445087,8444603,8443981,8444976,8443902,…,8420274]\n'
我漏了一行,我更新了我的问题。我如何将这个数字列表转换为帖子?检查我编辑的答案。无法复制-我得到
“[8444739,8445087,8444603,8443981,8444976,8443902,…,8420274]\n'
我漏掉了一行,我更新了我的问题。我如何将这个数字列表转换为帖子?检查我编辑的答案。我无法查看回复列表,为什么?@shankaran我猜不出你的实现,所以很难说。如果被迫下赌注,我会说你可能正在尝试迭代字符串中的字符,ra而不是使用例如
json.loads
t