基本上我正在創建一個用于學習目的的機器人,我想要做的是用戶添加帶有評論的逗號來旋轉評論,但是如果用戶添加評論框就像嘿,“驚人”“酷”,然后bot 用諸如“h”、“y”、“I”之類的隨機詞來評論,基本上它是隨機化第一個字符,
這里是代碼
def comment(driver, comment_custom,hashtags,count):
url = "https://www.instagram.com/explore/tags/" hashtags
driver.get(url)
wait = WebDriverWait(driver,10)
path = "/html/body/div[1]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]"
first_photo = wait.until(EC.presence_of_element_located((By.XPATH,path)))
first_photo.click()
time.sleep(1)
next_button1st = driver.find_element_by_xpath("/html/body/div[6]/div[1]/div/div/div/button")
next_button1st.click()
time.sleep(1)
for i in range (int(count)):
#comments on photo
path = "/html/body/div[6]/div[2]/div/article/div/div[2]/div/div/div[2]/section[3]/div/form/textarea"
comment = wait.until(EC.presence_of_element_located((By.CLASS_NAME,"Ypffh")))
comment.click()
time.sleep(1)
commet_text = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "Ypffh")))
comment = comment_custom[randint(0, len(comment_custom)-1)]
commet_text.send_keys(comment)
#post comment button
post_button = wait.until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Post')]")))
post_button.click()
time.sleep(1)
next_button2st = wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[6]/div[1]/div/div/div[2]/button")))
next_button2st.click()
這是 tkinter 的代碼
Comments_with_hash = Button(root, text="Comments_with_hashtags", height=1, width = 30,
command=lambda:comment(driver,comments.get(),hashtag.get(),likecount.get()))
Comments_with_hash.grid(padx=5,pady=5)
這是機器人的照片 https://prnt.sc/21k1nsr.png
我想要的預期評論
Nice and then Amazing and then amazing and nice basically in random
已編輯 無法隨機化睡眠時間基本上我想做的事用戶輸入 2 個數字(如 1,5)來隨機化每個操作之間的時間。 Bot 圖片:https : //prnt.sc/21nj6xm代碼我正在使用
time.sleep(randint(int(delay)))
錯誤
time.sleep(randint(int((delay))))
ValueError: invalid literal for int() with base 10: '1,5'
uj5u.com熱心網友回復:
我不能跑,但我想:
comment_custom是單個字串,因此使用索引comment_custom[index]可以從字串中獲得單個字符。
comment_custom = "hey,amazing,cool"
print( comment_custom[0] ) # char `h`
您必須將其轉換為字串/單詞串列,然后從串列中選擇單詞
comment_custom = "hey,amazing,cool"
words = comment_custom.split(',')
print( words[0] ) # string `hey`
你可以使用random.choice(words)代替words[random.randint(0, len(words)-1)]
所以你應該做
words = comment_custom.split(',')
comment = random.choice(words)
commet_text.send_keys(comment)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375695.html
