首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何发布电讯报的文章?

如何发布电讯报的文章?
EN

Stack Overflow用户
提问于 2018-02-16 17:36:14
回答 2查看 744关注 0票数 0

我想发表一篇使用Python和Telegraph API的Telegra.ph文章。我尝试过telegraph和python-telegraphapi模块,但我做不到。我尝试使用模块的示例代码:

代码语言:javascript
复制
from telegraph import Telegraph

telegraph = Telegraph()

telegraph.create_account(short_name='1337')

response = telegraph.create_page(
    'Hey',
    html_content='<p>Hello, world!</p>'
)

print('http://telegra.ph/{}'.format(response['path']))

这就是发生的事情:

代码语言:javascript
复制
File "AutoContent.py", line 9, in <module>
    html_content='<p>Hello, world!</p>'
  File "C:\Program Files\Python36\lib\site-packages\telegraph\api.py", line 168, in create_page
    'return_content': return_content
  File "C:\Program Files\Python36\lib\site-packages\telegraph\api.py", line 40, in method
    raise TelegraphException(response.get('error'))
telegraph.exceptions.TelegraphException: PAGE_SAVE_FAILED

另一个代码:

代码语言:javascript
复制
from telegraphapi import Telegraph
telegraph = Telegraph()
telegraph.createAccount("PythonTelegraphAPI")
page = telegraph.createPage("Hello world!", html_content="<b>Welcome, TelegraphAPI!</b>")
print('http://telegra.ph/{}'.format(page['path']))

接下来会发生什么:

代码语言:javascript
复制
File "AutoContent.py", line 6, in <module>
    page = telegraph.createPage("Hello world!", html_content="<b>Welcome, TelegraphAPI!</b>")
  File "C:\Program Files\Python36\lib\site-packages\telegraphapi\main.py", line 139, in createPage
    "return_content": return_content
  File "C:\Program Files\Python36\lib\site-packages\telegraphapi\main.py", line 32, in make_method
    post_request.json()['error'])
telegraphapi.exceptions.TelegraphAPIException: Error while executing createPage: PAGE_SAVE_FAILED

求求你救救我!如何使用Python发布一篇电讯报文章?

EN

回答 2

Stack Overflow用户

发布于 2018-02-20 20:04:36

我认为当你create_account的时候你应该换掉你的short_name

telegraph.create_account(short_name='<your_name>')

票数 0
EN

Stack Overflow用户

发布于 2019-05-13 01:52:02

使用API有两种不同的解决方案。

代码语言:javascript
复制
import json
MAIN_URL = 'https://api.telegra.ph/'

class apiuz():
    def __init__(self):
        self.http = requests.Session()

    def callMethod(self, n_method=None, a_method=None):
        xitoy2= MAIN_URL + n_method.__name__+'?'
        for x,y in a_method:
            if x!='self' and y!=None: xitoy2+=x+'='+str(y)+'&'
        response = self.http.get(xitoy2[:-1])
        xitoy2 = eval(response.text.replace('\/','/').replace('true','True').replace('false','False'))
        return xitoy2 

        #Methods created by @apiuz
    def createAccount(self, short_name=None, author_name=None, author_url=None):
        return self.callMethod(n_method=self.createAccount, a_method=locals().items())

    def editAccountInfo(self, access_token=None, short_name=None, author_name=None, author_url=None):
        return self.callMethod(n_method=self.editAccountInfo, a_method=locals().items())

    def getAccountInfo(self, access_token=None, field=None):
        return self.callMethod(n_method=self.getAccountInfo, a_method=locals().items())

    def revokeAccessToken(self, access_token=None):
        return self.callMethod(n_method=self.revokeAccessToken, a_method=locals().items())

    def createPage(self, access_token=None, title=None, author_name=None, author_url=None,
        content=None):
        return self.callMethod(n_method=self.createPage, a_method=locals().items())

    def editPage(self, access_token=None, path=None, title=None, content=None,
        author_name=None, author_url=None):
        return self.callMethod(n_method=self.editPage, a_method=locals().items())

    def getPage(self, path=None):
        return self.callMethod(n_method=self.getPage, a_method=locals().items())

    def getPageList(self, access_token=None, offset=0, limit=50):
        return self.callMethod(n_method=self.getPageList, a_method=locals().items())

    def getViews(self, path=None, year=None, month=None, day=None, hour=None):
        return self.callMethod(n_method=self.getPageList, a_method=locals().items())

#or

import requests

params = {
    'access_token': "<ACCES_TOKEN>",
    'path': '/Sample-Test-02-17-4',
    'title': 'My Title',
    'content':[ {"tag":"p","children":["A link to google",{"tag":"a","attrs":{"href":"http://google.com/","target":"_blank"},"children":["http://google.com"]}]} ],
    'author_name': 'My Name',
    'author_url': None,
    'return_content': 'true'
}

url = 'https://api.telegra.ph/editPage'

r = requests.post(url, json=params)
r.raise_for_status()
response = r.json()
print response```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48823781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档