0

これは、プログラミングのスキルがある人にとっては簡単なことだと思います(私とは異なります)。Google Sites API をいじっています。基本的に、面倒な遅い Web フォームを使用して 1 つずつページを作成するのではなく、一連のページをバッチで作成できるようにしたいと考えています。

必要なファイルをすべてインストールし、ドキュメントを読み、このサンプル ファイルを正常に実行しました。実際のところ、このサンプル ファイルには、Google サイトでページを作成するための Python コードが既に含まれています。

elif choice == 4:
      print "\nFetching content feed of '%s'...\n" % self.client.site

      feed = self.client.GetContentFeed()
      try:
        selection = self.GetChoiceSelection(
            feed, 'Select a parent to upload to (or hit ENTER for none): ')
      except ValueError:
        selection = None

      page_title = raw_input('Enter a page title: ')

      parent = None
      if selection is not None:
        parent = feed.entry[selection - 1]

      new_entry = self.client.CreatePage(
          'webpage', page_title, '<b>Your html content</b>',
          parent=parent)
      if new_entry.GetAlternateLink():
        print 'Created. View it at: %s' % new_entry.GetAlternateLink().href

ページの作成は、page_title と new_entry と CreatePage を中心に展開することを理解しています。ただし、一度に 1 つのページを作成するのではなく、多くのページを作成したいと考えています。

私はいくつかの調査を行い、次のようなものが必要だと収集しました

page_titles = input("Enter a list of page titles separated by commas: ").split(",")

ページ タイトルのリストを収集する (page1、page2、page3 など -- テキスト エディタまたはスプレッドシートを使用して、コンマ区切りの名前の長いリストを生成する予定です)。

現在、その文字列を取得して new_entry に「フィード」し、文字列の値ごとに個別のページを作成する方法を見つけようとしています。その方法がわかりません。誰でも助けてもらえますか?

それが役立つ場合、これは Google API がページを作成するために必要なものです:

entry = client.CreatePage('webpage', 'New WebPage Title', html='<b>HTML content</b>')
print 'Created. View it at: %s' % entry.GetAlternateLink().href

ありがとう。

4

2 に答える 2

0

「そのリストを使用して、各値で必要な回数だけコマンドを実行する」場合はいつでも、それはforループです。(呼び出しやリスト内包表記などで、暗黙の for ループになることもありますが、mapそれでもループです。)

したがって、この後:

page_titles = raw_input("Enter a list of page titles separated by commas: ").split(",")

これをして:

for page_title in page_titles:
    # All the stuff that has to be done for each single title goes here.
    # I'm not entirely clear on what you're doing, but I think that's this part:
    parent = None
    if selection is not None:
        parent = feed.entry[selection - 1]

    new_entry = self.client.CreatePage(
        'webpage', page_title, '<b>Your html content</b>',
        parent=parent)
    if new_entry.GetAlternateLink():
        print 'Created. View it at: %s' % new_entry.GetAlternateLink().href

通常はこれですべてです。

于 2013-07-02T22:02:56.567 に答える
0

for ループを使用して dict オブジェクトをループし、複数のページを作成できます。ここでは、開始するための小さなスニペットを示します。

import gdata.sites.client

client = gdata.sites.client.SitesClient(
        source=SOURCE_APP_NAME, site=site_name, domain=site_domain)

pages = {"page_1":'<b>Your html content</b>',
         "page_2":'<b>Your other html content</b>'}

for title, content in pages.items():
    feed = client.GetContentFeed()

    parent = None
    if selection is not None:
        parent = feed.entry[selection - 1]

    client.CreatePage('webpage', page_title, content, parent=parent)

外部ソースからスクリプトをフィードする場合は、csv ファイルのようなものをお勧めします

##if you have a csv file called pages.csv with the following lines
##page_1,<b>Your html content</b>
##page_2,<b>Your other html content</b>

import csv

with open("pages.csv", 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        title, content = row
        client.CreatePage('webpage', title, content, parent=parent)
于 2013-07-02T21:29:56.090 に答える