Python - Return 语句清空我的列表

标签 python selenium return

我目前正在开发一个 selenium 机器人,它从 1000 个名词列表中随机获取一个英语名词,将其放入一个获取类似 Instagram 标签的网站中,然后进入 Instagram,登录我的帐户,并开始喜欢图片使用该主题标签,然后重新开始该过程

我的问题是返回相似主题标签的函数,这是它的代码:

def get_similar_tags(tag):
    url = "https://top-hashtags.com/hashtag/" + str(tag).lower() + "/"
    page = requests.get(url).text
    parsed_page = BeautifulSoup(page, "html.parser")
    parsed_page = parsed_page.find("div", class_="tht-tags")
    text = parsed_page.text
    hashtag = text.split(" ")
    hashtag = [s.strip('#') for s in hashtag]
    hashtag = hashtag.pop(len(hashtag) - 1)
    return hashtag

当我尝试在函数内部打印主题标签变量时,输出会填充主题标签,这正是我所需要的。

当我使用该函数将主题标签变量分配给另一个变量时,如下所示:

foo = get_similar_tags(random_noun)

然后我就这么做了

print(foo)

我什么也没得到,甚至没有一个空列表,只是一个空行。 我已经尝试使用全局变量分配 get_similar_tags 函数返回的值,但这也不起作用。

任何帮助都会非常感激, 谢谢

最佳答案

hashtag = hashtag.pop(len(hashtag) - 1)

不要这样做,尝试一下:

hashtag.pop(len(hashtag) - 1)

list.pop([i])

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

https://docs.python.org/3/tutorial/datastructures.html

关于Python - Return 语句清空我的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58460797/

相关文章:

python - Python 模块中的命名空间与 Flask 发生冲突

Python 格式和 Pandas

java - 使用 Java for 循环中的 Xpath

java - 返回另一个类的时间

java - 交换后如何返回数组?

python - 名称错误 : name 'helloworld' is not defined

python - 滴答函数绘图器

selenium - 无法检索文本 Selenium

selenium - 如何处理使用 WebDriver 进行页面导航时出现的警报?

swift - 如何减少此 Swift 函数中返回 false 的次数