0

我正在使用 Instapy 创建以下脚本:

#!/usr/bin/env python3

from instapy import InstaPy

number = int(input("How many Number of people you want to follow: "))
user = []
for i in range(0,number):
    name = input("Enter Username to interact: ")
    user.append(name)
session = InstaPy(username=" ",password=" ")
session.login()
#Used to Follow the User
session.set_do_follow(enabled=True, percentage=100)
#Used to comment on a post
session.set_comments(["Cool", "Super!"])
session.set_do_comment(enabled=True, percentage=80)
#Used to Like the Post of the User
session.set_do_like(True, percentage=70)
#Used to Interact with the User
session.interact_by_users(user, amount=5,randomize=True, media='Photo')
#Used to insteract by Comments
session.set_use_meaningcloud(enabled=True, license_key=' ', polarity="P")
session.set_use_yandex(enabled=True, API_key=' ', match_language=True, language_code="en")
session.set_do_reply_to_comments(enabled=True, percentage=100)
session.set_comment_replies(replies=[u"", u"", u"", "", u"", u"‍♂️", u"", u"",  u"",  u"", u"", u""],media="Photo")
session.set_do_like(enabled=True, percentage=94)
session.interact_by_comments(user, posts_amount=10, comments_per_post=5, reply=True, interact=True, randomize=False, media="Photo")
session.end()

每当我尝试运行脚本时,都会显示以下错误:

Oh no! Failed to get the list of supported languages by Yandex Translate :( ~text language won't be matched

link_elems error local variable 'post_href' referenced before assignment

主要问题是在帖子上写评论。

4

1 回答 1

0

我在 Instapy 评论中发现了几个问题。这些我可以帮你。

  • 如果提到任何其他帐户,例如@nizamudheen_at,评论将不起作用
  • 如果太长,评论会中断。

这些可以通过稍微修改comment_util.py (Python37_64\Lib\site-packages\instapy)来快速修复。

instapy 的一个缺点是它总是使用“Enter”按钮来发表评论。所以我们需要调整它,让它通过点击“发布”来评论。其他明智的长评论将不起作用。

为此:在 comment_util.py 中添加一个函数,例如:

def get_comment_post(browser):
    comment_post = browser.find_elements_by_xpath(
        read_xpath(get_comment_post.__name__, "comment_post")
    )

    return comment_post

在 comment_image 函数中,确保在 try 中添加第一个 if 语句。这将确保您是否提及任何帐户,并且如果评论较长,则可以正确处理:

def comment_image(browser, username, comments, blacklist, logger, logfolder):
    """Checks if it should comment on the image"""
    # check action availability
    if quota_supervisor("comments") == "jump":
        return False, "jumped"

    rand_comment = random.choice(comments).format(username)
    rand_comment = emoji.demojize(rand_comment)
    rand_comment = emoji.emojize(rand_comment, use_aliases=True)

    open_comment_section(browser, logger)
    # wait, to avoid crash
    sleep(3)
    comment_input = get_comment_input(browser)
    comment_post = get_comment_post(browser)
    logger.info("{} pooooost".format(comment_post))
    logger.info("{} pooooost".format(comment_post[0]))

    try:
        if len(comment_input) > 0:
            # wait, to avoid crash
            sleep(2)
            comment_input = get_comment_input(browser)
            logger.info("{} .....".format(comment_input))
            logger.info("{} input[0]".format(comment_input[0]))
            # below, an extra space is added to force
            # the input box to update the reactJS core
            comment_to_be_sent = rand_comment

            # wait, to avoid crash
            sleep(2)
            # click on textarea/comment box and enter comment
            
            # wait, to avoid crash
            sleep(1)
            if '@' in comment_to_be_sent:
                (
                    ActionChains(browser)
                    .move_to_element(comment_input[0])
                    .click()
                    .send_keys(comment_to_be_sent)
                    .pause(3)
                    .move_to_element(comment_post[0])
                    .click()
                    .perform()
                )
            else:            
                
                # post comment / <enter>
                (
                    ActionChains(browser)
                    .move_to_element(comment_input[0])
                    .send_keys(Keys.ENTER)
                    .perform()
                )

            update_activity(
                browser,
                action="comments",
                state=None,
                logfolder=logfolder,
                logger=logger,
            )

最后但并非最不重要的一点是,添加 xpath 函数来处理“发布”按钮:在 xpath_compile.py

xpath["get_comment_post"] = {
    "comment_post": "//button[text()='Post']",
    
}
于 2020-12-03T08:52:06.580 回答