微信咨询

微信咨询

13610*910*9

服务热线 7*24小时

电话咨询

selenium +PhantomJS自动发布带图文微博(涉及图片上传 autoit操作)

iamdu2020-01-10 12:38:56 浏览: 170166
from selenium import webdriver
from bs4 import BeautifulSoup
import time
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

browser = webdriver.Chrome()
# browser = webdriver.PhantomJS()
# headers = {
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#     'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
#     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4',
#     'Connection': 'keep-alive'
# }
 
# cap = DesiredCapabilities.PHANTOMJS.copy()#使用copy()防止修改原代码定义dict
 
# for key, value in headers.items():
#     cap['phantomjs.page.customHeaders.{}'.format(
#         key)] = value
 
# browser = webdriver.PhantomJS(desired_capabilities=cap)
print('hello1')
browser.get('https://weibo.com')
time.sleep(5) #给页面加载预留足够的时间
# print(browser.page_source)
# PhantomJS默认虽然的确使用了窗口,但是默认窗口很小。导致很多元素不可见,加上最大化解决Screenshot: available via screen错误
browser.maximize_window()
print('hello2')
browser.find_element_by_css_selector("#loginname").send_keys("你的账号")
browser.find_element_by_css_selector(".info_list.password input[node-type='password']").send_keys("你的密码")
browser.find_element_by_css_selector(".info_list.login_btn a[node-type='submitBtn']").click()
time.sleep(5) #给页面加载预留足够的时间
# print(browser.page_source)
soup = BeautifulSoup(browser.page_source,'html.parser')
title = soup.find(class_='nameBox').find(class_='name S_txt1').text
print(title)
#自动发布微博
browser.maximize_window()
browser.find_element_by_class_name("send_weibo").find_element_by_class_name("W_input").send_keys("带图上传")
browser.find_element_by_css_selector("a[action-type='multiimage']").click()
time.sleep(2)
c = 'pic.exe'
os.system(c)
# Runtime.getRuntime().exec('pic.exe')
time.sleep(2)
browser.find_element_by_class_name("W_btn_a").click()
print('发布成功了老弟')
browser.close() # 关闭浏览器 # 关闭浏览器

 

案例参考了:

https://blog.csdn.net/xiao1_1bing/article/details/78926239

autoit操作

https://blog.csdn.net/weixin_42024694/article/details/80080629