使用 Ruby 或 Python 等语言与 API 交互

发布于 2024-12-08 21:11:38 字数 556 浏览 1 评论 0原文

我希望开始构建网络应用程序。我需要基本的 CRUD 功能。

我了解如何在 ruby​​ 和 python 中执行此操作的基础知识 - 但这就是我陷入困境的地方:

How do I parse a GET or POST request in Ruby/Python?

我正在尝试与其他人的 API 进行交互,我知道我需要的字符串,只是不知道如何在编码时引用和使用该字符串。

例子: 我想引用用户个人资料,并计算他们发布的帖子数量。

我知道 URL 看起来像这样: http://www.coolwebsite.com/users?bobsgreat?postcount

返回一个value= 他的帖子。

如何将这个数字放入我的 Python 脚本中,然后如何让 Python 将其写入 HTML 文件中以供查看?

抱歉,如果我在这里问了很多问题,随着我开始写作,这个问题也越来越多。

I'm looking to start building web apps. I need basic CRUD functionality.

I understand the basics of how to do this in ruby and python - but here's where I'm stuck:

How do I parse a GET or POST request in Ruby/Python?

I'm trying to interact with someone else's API, I know the string I need, just not how to reference and use that string while coding.

Example:
I want to reference a users profile, and count the number of posts they've made.

I know the URL looks something like this:
http://www.coolwebsite.com/users?bobsgreat?postcount

That returns a value= to his posts.

How do I get this number into my Python script- and then how can I have python write this into an HTML file for viewing?

Sorry if I'm asking a lot here, the question grew as I started writing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空袭的梦i 2024-12-15 21:11:38

这是一个非常粗略的示例,说明如何在 python 中执行此操作。它需要根据您的具体情况进行修改,但希望它可以帮助您开始:

import urllib
import urllib2
url = "http://www.coolwebsite.com"
post = urllib.urlencode({"users":"bob", "bobsgreat":"yes", "postcount":"14"})

request = urllib2.Request(url, post)
socket = urllib2.urlopen(request)

hdrs = socket.headers
source = socket.read()
socket.close()

print "---- Headers -----"
print hdrs
print "---- Source HTML -----"
print source
print "---- END -----"

value = 0
for line in source.splitlines():
    if not line.strip():  continue
    if line.startswith("value="):
        try:
            value = line.split("=")
        except IndexError:
            pass
    if value > 0:
        break

open("some.html", "w").write("value is: %d" % value)

This is a very rough example of how you can do this in python. It would need modification for your specific situation, but hopefully it can get you started:

import urllib
import urllib2
url = "http://www.coolwebsite.com"
post = urllib.urlencode({"users":"bob", "bobsgreat":"yes", "postcount":"14"})

request = urllib2.Request(url, post)
socket = urllib2.urlopen(request)

hdrs = socket.headers
source = socket.read()
socket.close()

print "---- Headers -----"
print hdrs
print "---- Source HTML -----"
print source
print "---- END -----"

value = 0
for line in source.splitlines():
    if not line.strip():  continue
    if line.startswith("value="):
        try:
            value = line.split("=")
        except IndexError:
            pass
    if value > 0:
        break

open("some.html", "w").write("value is: %d" % value)
老街孤人 2024-12-15 21:11:38

如果您必须处理 API 并使用 Ruby,我强烈推荐 api_smith gem。上次我不得不这样做时,我花了大约 2 个小时左右的时间来完成整个 API。以下博客文章提供了很好的介绍:

构建使用 API Smith 构建结构化 API 客户端

If you have to deal with an API and use Ruby, I highly recommend the api_smith gem. Last time I had to do it it took me like 2 hours or so to wrap up an entire API. The following blog post gives a good introduction:

Building Structured API Clients with API Smith

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文