You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在条件为真时重复执行Python代码以获取合规维基百科标题

修改后的代码实现需求

调整原代码的while循环逻辑,移除直接终止循环的break,改为判断标题中是否包含'משתמש',仅当标题不包含该字符串时才退出循环,否则持续获取随机维基页面:

from typing import Text
import tweepy
import requests
from bs4 import BeautifulSoup
import webbrowser

from tweepy import Response, tweet
client = tweepy.Client(
    consumer_key="XXXXXX",
    consumer_secret="XXXXXX",
    access_token="XXXXXX",
    access_token_secret="XXXXXX"
)

while True:
    url = requests.get("https://he.wikipedia.org/wiki/Special:Random")
    soup = BeautifulSoup(url.content, "html.parser")
    title = soup.find(class_="firstHeading").text
    print(title)
    # 标题不含'משתמש'时退出循环
    if 'משתמש' not in title:
        break

try:
   client.create_tweet(text="לשטח את " + title)
   print("וואלה יופי הצלחת יא בתול")
   print("יא קינג יא אלוף אין עליך יא גאון מחשבים")
except tweepy.errors.TweepyException as e:
   print("שגיאה על הראש שלך")
   print("יא אפס")

关键改动说明

  • 替换原循环内的break为条件判断,实现"含指定字符串则重复获取"的逻辑
  • 调整导入语句顺序,让代码结构更规范
  • 移除无意义的printed = print(title)赋值,直接执行打印操作

内容的提问来源于stack exchange,提问作者toma0711

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:39:56