网易云音乐自动评论
接口评论被加密,不熟悉加密方式为前提下,建议直接使用selenium实现自动评论
import time
import random
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
class WyySay:
"""
参考 https://www.cnblogs.com/testway/p/16676195.html
下载浏览器对应驱动 http://chromedriver.storage.googleapis.com/index.html
selenium:4.7.2 3.x不兼容建议使用4.x
cmd启动服务 start chrome --flag-switches-begin --flag-switches-end --remote-debugging-port=9887
运行代码,登录网易云音乐账户,可设置评论间隔,防止长时间多评论账号ip被检测为恶意程序
"""
def __init__(self):
s = Service(executable_path="chromedriver.exe")
self.chrome_options = Options()
self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9887")
self.driver = webdriver.Chrome(service=s, options=self.chrome_options)
# 待评论音乐ID
self.liste_des_commentaires_url = ["2011097172", "29774609", "1973141817"]
# 待评论内容
self.test = ["好听!!!", "666!", "呐喊!"]
def run(self):
for des_commentaires_url in self.liste_des_commentaires_url:
self.driver.get("https://music.163.com/#/song?id=" + des_commentaires_url)
time.sleep(2)
self.driver.switch_to.frame("contentFrame")
time.sleep(2)
self.driver.find_element(By.XPATH, '//*[@class="u-txtwrap holder-parent f-pr"]').click()
time.sleep(2)
self.driver.find_element(By.XPATH, '//*[@class="u-txt area j-flag"]').send_keys(
random.choice(self.test))
time.sleep(2)
# 这里很奇怪 必须往下拉滚轮才可以定位发送评论元素,不然找不到发送评论元素
tag = self.driver.find_element(By.XPATH, '//*[@class="iptarea"]')
self.driver.execute_script("arguments[0].scrollIntoView();", tag) # 拖动到可见的元素去
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@class="btns f-cb f-pr"]/a').click()
time.sleep(2)
if __name__ == '__main__':
while True:
# time.sleep(1800)
try:
s = WyySay()
s.run()
except BaseException as e:
print(e)
标签:__,网易,self,音乐,driver,评论,time,sleep
From: https://www.cnblogs.com/chron/p/17123225.html