如何投递电报文章?

2024-04-20 10:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想发布一个Telegra.ph.公司文章使用Python和Telegraph API。我试过电报和python telegraphapi模块,但我做不到。我尝试使用模块的示例代码:

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']))

接下来是:

^{pr2}$

另一个代码:

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']))

发生了什么:

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发布电报文章?在


Tags: inpyhelloworldhtmlline文章page
2条回答

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

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```

我认为您应该在您的create_account时替换{}:

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

相关问题 更多 >