返回首页
  uu快三外挂 > 编程 > uu快三骗局 >

Python Selenimu 实现雅虎问答自动发布

2019-03-06 10:00 阅读:86 来源:智宇SEO自媒体

  在操作一个外贸项目,需要从facebook,twitter,雅虎上引流。前面两个自动发布代码已经写了,今天发雅虎的自动发布代码。

# -*- coding: utf-8 -*-
# @Time         : 2019/3/05 14:52
# @Author       : Zhiyu
# @File         : reddit.py.py
# @Software     : PyCharm
# @Description  : Reddit 分享类

import time,os,io,json,platform,math,random,re
from selenium.webdriver.common.by import By
from selenium import webdriver
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


class Yahoo:
    """
    分享到Twitter
    """
    # 当前根目录
    root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    driver = None
    def __init__(self , debug=False):
        """
        初始化
        :param debug:
        """
        # 启动chrome
        try:
            # chrome 配置项
            chrome_options = webdriver.ChromeOptions()

            executable_path = self.root + '/chrome/chromedriver'  # Linux使用
            if platform.system() != 'Linux':
                executable_path = executable_path + '.exe'  # window使用exe
                chrome_options.add_argument("--proxy-server=http://127.0.0.1:1080")
            else:
                chrome_options.add_argument("--proxy-server=http://192.168.31.173:1080")

            if debug == False:
                chrome_options.add_argument('--headless')
                chrome_options.add_argument('--no-sandbox')
                chrome_options.add_argument('--disable-gpu')
                chrome_options.add_argument('--disable-setuid-sandbox')
                chrome_options.add_argument('--disable-dev-shm-usage')

            # 初始化chrome
            self.driver = webdriver.Chrome(executable_path, 0, chrome_options)
            # 屏幕最大化
            self.driver.maximize_window()
            # 加载cookie前 需要先请求该域名任意链接
            self.driver.get("http://hk.answers.yahoo.com/?src=hr_trough")

            # 读取COOKIE 并加如到浏览器中
            f = io.open(self.root + '/cookie/yahoo', 'r', encoding='utf-8')
            cookies = json.loads(f.read())
            for cookie in cookies:  # 添加cookie 字典
                self.driver.add_cookie(cookie)

            time.sleep(1)
        except Exception, e:
            print e
            print 'repr(e):	', repr(e)
            ret = None
            print "初始化失败!"
            exit()
    def is_login(self):
        try:
            xpath = '//*[@id="yui_3_10_3_1_1375219693637_127"]/b'
            text = self.driver.find_element_by_xpath(xpath).text
            return True
        except:
            return False

    def auto_post(self , title , description):
        try:
            url = "http://hk.answers.yahoo.com/dir/index?sid=396545018"
            self.driver.get(url)
            time.sleep(1)

            if self.is_login() == False:
                print "登入失败,检查Cookie是否过期。"
                self.close()


            self.driver.find_element_by_css_selector('div.ask-details.Maw-75.W-100.Va-m.Fl-n.Cur-p.D-ib.Mt-3.Whs-nw.Ov-h.Ol-n').click()
            time.sleep(1)

            title_area = self.driver.find_element_by_xpath("//textarea[@name='title']")
            title_area.send_keys(title)
            time.sleep(2)
            print("send title")

            title_area = self.driver.find_element_by_xpath("//textarea[@name='details']")
            title_area.send_keys(description)
            time.sleep(2)
            print("send description")

            postbutton = self.driver.find_element_by_xpath("//input[@data-ylk='slk:submit']")
            postbutton.click()
            time.sleep(2)

            # postbutton = self.driver.find_element_by_xpath("//input[@data-ylk='slk:submit']")
            # postbutton.click()

            print("post done")
            ret = True
        except Exception ,e:
            print 'repr(e):	', repr(e)
            ret = False

        self.close()
        return  ret

    def close(self):
        self.driver.close()
        self.driver.quit()
        self.driver = None



返回首页