开启左侧

数据采集:自动化运营微博实战

[复制链接]
在线会员 8bnCTDN 发表于 2022-12-31 12:40:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
功用构成

    主动登岸减存眷打消存眷写批评收微专
开辟东西

    版原
    Python 3.6.5
    浏览器
    chrome(装置 chromedriver)
    相干模块
    selenium
    time
主动登岸
  1. from selenium import webdriver
  2. import time
  3. browser = webdriver.Chrome()
  4. # 登岸微专
  5. def weibo_login(username, password):
  6.     #翻开 微专登岸界里
  7.     browser.get('https://passport.weibo.cn/signin/login')
  8.     # 隐性等候。隐形等候是树立了一个最少等候时间,假设正在划定时间内乱网页减载完毕,则施行下一步,不然不竭比及时间停止。
  9.     # 隐性等候对于全部driver的周期皆起感化,以是只要树立一次便可。
  10.     browser.implicitly_wait(5)
  11.     time.sleep(1)
  12.     # 挖写登岸疑息:用户名,暗码
  13.     browser.find_element_by_id('loginName').send_keys(username)
  14.     browser.find_element_by_id('loginPassword').send_keys(password)
  15.     time.sleep(1)
  16.     # 面打登岸
  17.     browser.find_element_by_id('loginAction').click()
  18.     time.sleep(1)
  19. # test
  20. #自己 微专的账号暗码
  21. username = '奸淫奸淫**'
  22. password = '奸淫奸淫**'
  23. weibo_login(username,password)
复造代码
减存眷
  1. # 增加指定用户
  2. def add_follow(uid):
  3.     browser.get('https://m.weibo.com/u/' + str(uid))
  4.     time.sleep(1)
  5.     follow_button = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div[4]/div[2]/div/div[1]/div')
  6.     follow_button.click()
  7.     time.sleep(1)
  8.     # 挑选分组
  9.     group_button = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div[3]/div[2]/footer/div[1]/a')
  10.     group_button.click()
  11.     time.sleep(1)
  12. # test
  13. # 增加指定用户
  14. uid = '5520391962'
  15. add_follow(uid)
复造代码
打消存眷
  1. # 打消存眷指定用户
  2. def delete_follow(uid):
  3.     browser.get('https://m.weibo.com/u/' + str(uid))
  4.     time.sleep(1)
  5.     delete_button = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div[4]/div[2]/div/div[1]/div/span/i').click()
  6.     time.sleep(1)
  7.     delete_button = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div[4]/div[2]/div/div[1]/div/div/ul/li[2]/div/h4').click()
  8.     time.sleep(1)
  9.     delete_button = browser.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div[2]/footer/div[2]/a').click()
  10.     time.sleep(1)
  11. # test
  12. # 打消存眷指定用户
  13. uid = '5520391962'
  14. delete_follow(uid)
复造代码
写批评
  1. # 给指定某条微专增加实质
  2. def add_co妹妹ent(weibo_url, content):
  3.     browser.get(weibo_url)
  4.     browser.implicitly_wait(5)
  5.     content_textarea = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div/div[5]/div/div[1]').click()
  6.     content_textarea = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div/div[5]/div/div/div/div[1]/textarea[1]').clear()
  7.     time.sleep(1)
  8.     content_textarea = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div/div[5]/div/div/div/div[1]/textarea[1]').send_keys(content)
  9.     time.sleep(2)
  10.     co妹妹ent_button = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div/div[5]/div/div/div/div[2]/button').click()
  11.     time.sleep(1)
  12. # test
  13. # 给指定微专写批评
  14. weibo_url = 'https://m.weibo.cn/detail/4161433421251081'
  15. content = '尝试批评'
  16. add_co妹妹ent(weibo_url,content)
复造代码
收微专
  1. # 收笔墨微专
  2. def post_weibo(content):
  3.     # 跳转到用户的尾页
  4.     browser.get('https://weibo.com')
  5.     browser.implicitly_wait(5)
  6.     # 面打左上角的公布按钮
  7.     post_button = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/div[1]/div[2]/div[2]').click()
  8.     # 正在弹出的文原框中输出实质
  9.     content_textarea = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div/main/div[1]/div/span/textarea[1]').send_keys(content)
  10.     time.sleep(2)
  11.     # 面打公布按钮
  12.     post_button = browser.find_element_by_xpath('//*[@id="app"]/div[1]/div/header/div[3]/a').click()
  13.     time.sleep(1)
  14. # test
  15. # 主动收微专
  16. content = '尝试'
  17. post_weibo(content)
复造代码
写正在以后

更多实质,欢送存眷公家号「Python空间」,等候战您的交换

数据收罗:主动化经营微专真战-1.jpg
您需要登录后才可以回帖 登录 | 立即注册 qq_login

本版积分规则

发布主题
阅读排行更多+
用专业创造成效
400-778-7781
周一至周五 9:00-18:00
意见反馈:server@mailiao.group
紧急联系:181-67184787
ftqrcode

扫一扫关注我们

Powered by 职贝云数A新零售门户 X3.5© 2004-2025 职贝云数 Inc.( 蜀ICP备2024104722号 )