示例#1
0
def main():
    br = Browser('chrome')
    br.visit(URL)
    sleep(3)
    if br.is_text_present("Remember me", wait_time=7):
        br.fill('username', NAME)
        br.fill('password', PASSWORD)
        br.execute_script('javascript:savesettings()')
示例#2
0
def main():
    br = Browser('chrome')
    br.visit(URL)
    sleep(3)
    if br.is_text_present("Remember me", wait_time=7):
        br.fill('username', NAME)
        br.fill('password', PASSWORD)
        br.execute_script('javascript:savesettings()')
示例#3
0
def form_fill(user, form):
	display = Display(visible=0, size=(800, 600))
	display.start()
	if form.email != {}:
		st = emailFill(user, form)
		email_send("Software Internship Application",st,[form.email['email'], user.getField(2)],attachment=user.getField(6))
		return
	browser = Browser()
	browser.visit(form.url)
	if form.greenHouseResume:
		string = """$("#application_form").append('<input type="hidden" id="resume_url" name="job_application[resume_url]" value=""" + '"' + user.getField(6.1) + '"' + """><input type="hidden" id="resume_url_filename" name="job_application[resume_url_filename]" value="resume.pdf">')"""
		browser.execute_script(string)
	def doClicks(): #partial_htmls being dealt with
		for ele in form.html_button_clicks:
			browser.find_link_by_partial_href(ele)[0].click()
	def fillField():
		for ele, tple in form.Fill_Fields.iteritems():
			browser.fill(ele,user.getField(tple[0]))
	def attachField():
		for ele, field in form.Attach_Fields.iteritems():
			browser.attach_file(ele,os.getcwd() + "/" + user.getField(field))
	def dropDownField():
		for ele, index in form.Drop_Down_Fields.iteritems():
			browser.execute_script('document.getElementById("' + ele + '")[' + str(index) + '].selected=true')
	def dropDownHelper():
		for ele,index in form.Drop_Down_Fields_Helper.iteritems():
			browser.execute_script('document.getElementsByTagName("'+ele+'")['+str(index)+'].selected=true')
	def checkBoxes():
		for ele in form.CheckBoxes:
			browser.execute_script('document.getElementById("' + ele + '").checked=true')
	def doJS():
		for ele in form.extraJavaScript:
			browser.execute_script(ele)
	def doJSHelp():
		for ele, index in form.extraJSHelper.iteritems():
			browser.execute_script('document.getElementById("' + ele +'").value = "'+ user.getField(index) +'";')
	def submitJS():
		for ele in form.submitJS:
			browser.execute_script(ele)
	doClicks()
	fillField()
	attachField()
	dropDownField()
	dropDownHelper()
	checkBoxes()
	doJS()
	doJSHelp()
	submitJS()
	if not form.submitJS:
		browser.execute_script('document.getElementsByClassName("' + form.finalSubmit + '")[document.getElementsByClassName("' + form.finalSubmit + '").length-1].click()')
	time.sleep(10)
	browser.quit()
	display.stop()
示例#4
0
class TemMail():
    def __init__(self):
        self.driver_name = 'chrome'
        self.executable_path = '/usr/local/bin/chromedriver'

        self.driver = Browser(driver_name=self.driver_name,
                              executable_path=self.executable_path)
        self.driver.driver.set_window_size(800, 800)
        self.driver.visit('https://www.moakt.com')
        self.driver.find_by_name('random').click()
        while self.driver.url == 'https://www.moakt.com/zh/mail':
            return

    def getMailName(self):
        sleep(1)
        self.driver.execute_script('changeAddress();')
        sleep(1)
        mailname = self.driver.find_by_id('email-address').text
        return mailname

    def getEmail(self):

        while True:
            self.driver.find_by_text(u"刷新列表").click()
            temdata = self.driver.find_by_text(u"激活您的ProcessOn帐号")
            if len(temdata) == 0:
                sleep(2)
            else:
                em = self.driver.find_by_text(u"激活您的ProcessOn帐号").first
                url = em["href"] + '/content'
                self.getEmailDea(url)
                break

    def getEmailDea(self, emlurl):

        self.driver.visit(emlurl)
        temtext = self.driver.html
        url_verify = re.findall(
            r"https://www.processon.com/signup/verification/\w+", temtext)
        ss_mail = requests.Session()
        rsp_verify = ss_mail.get(url_verify[0])

        if rsp_verify.status_code == 200:
            self.driver.quit()
        else:
            print("failure:" + rsp_verify.status_code)
示例#5
0
from splinter.browser import Browser
#from time import sleep

b = Browser('firefox')
b.visit('http://www.baidu.com')
#b.fill('wd', 'test\r')
#b.fill('wd', 'test\n')
#b.fill('wd', 'test\r\n')

b.fill('wd', 'test')
#b.execute_script('document.getElementsByName("f")[0].submit()')

script = '''
    var e = document.createEvent('KeyboardEvent');
    e.initKeyEvent('keypress', true, true, window, false, false, false, false, 13, 0);
    document.getElementsByName('wd')[0].dispatchEvent(e);
'''
b.execute_script(script)
#sleep(5)
b.type('wd', ' python')
#b.type('wd', ' python', slowly=True) # slowly not working
示例#6
0
def huoche():
    global b
    b = Browser(driver_name="chrome")
    b.visit(ticket_url)
    b.execute_script('alert("开始刷票喽~~~~")')
    sleep(2)
    b.get_alert().dismiss()

    while b.is_text_present(u"登录"):
        sleep(1)
        login()
        if b.url == initmy_url:
            break

    try:
        #跳回购票页面
        b.visit(ticket_url)

        #加载查询信息
        b.cookies.add({"_jc_save_fromStation": starts})
        b.cookies.add({"_jc_save_toStation": ends})
        b.cookies.add({"_jc_save_fromDate": dtime})
        b.reload()
        i = 1

        #循环点击预订
        if order != 0:
            while b.url == ticket_url:
                b.find_by_text(u"查询").click()
                #                b.find_by_text(ttype).click()

                if b.find_by_text(u"预订"):
                    sleep(0.3)
                    b.find_by_text(u"预订")[order - 1].click()
                    print b.url

                    if b.is_text_present(u"证件号码", wait_time=0.2):
                        #                        print [ i.text for i in b.find_by_text(pa) ]
                        b.find_by_text(pa)[1].click()

                else:
                    b.execute_script('alert("似乎没有可预订选项")')
                    b.get_alert().dismiss()
                    pass

        else:
            while b.url == ticket_url:
                b.find_by_text(u"查询").click()
                if b.find_by_text(u"预订"):
                    sleep(0.3)
                    for i in b.find_by_text(u"预订"):
                        i.click()
                        sleep(0.1)
                        if b.is_text_present(u"证件号码"):
                            b.find_by_text(pa)[1].click()

                    else:
                        b.execute_script('alert("似乎没有可预订选项")')
                        b.get_alert().dismiss()
                        pass

        b.execute_script('alert("能做的都做了")')
        b.get_alert().dismiss()

        print u"能做的都做了.....不再对浏览器进行任何操作"

    except Exception:
        print u"出错了...."
示例#7
0
class Amazon(object):
    def __init__(self, keyword):
        self.url = "https://www.amazon.com"
        self.name = "".join(random.sample(string.ascii_letters + string.digits, 6))
        self.email = "".join(random.sample(string.ascii_letters + string.digits, 16)) + "@163.com"
        self.password = "".join(random.sample(string.ascii_letters + string.digits, 8))
        self.keyword = keyword
        self.chrome_options = Options()
        logging.basicConfig(filename='amazon.log', level=logging.DEBUG)
        self.conn = pymysql.connect(host='192.168.0.211', port=6033, user='******', password='******', db='amazon')
        self.cs = self.conn.cursor()
        self.cs.execute('select id,ip,port from register_proxy_ips where is_alived = 1 order by id desc limit 1')
        self.proxy_ip = self.cs.fetchone()
        self.cs.execute('update register_proxy_ips set is_alived = 0 where id = %s' % self.proxy_ip[0])
        self.conn.commit()
        logging.debug(self.proxy_ip)
        # self.chrome_options.add_argument('--proxy-server=http://{host}:{port}'.format(host=self.proxy_ip[1], port=self.proxy_ip[2]))
        self.browser = Browser('chrome', user_agent=generate_user_agent(device_type='desktop'))

    def run(self):
        self.acess()
        self.search_kw()
        i = 1
        while i < 7:
            ad_list = self.browser.find_by_xpath('//li[contains(@class, "AdHolder")]')
            if ad_list:
                ad_list[0].find_by_tag('h2').click()
                break
            else:
                time.sleep(300)
                self.search_kw()
                i += 1
        if i > 6:
            self.browser.quit()
        self.register()
        self.add_pay_method()
        self.add_address()
        self.buy_goods()
        self.add_list()

    def acess(self):
        self.browser.visit(self.url)
        logging.error("代理IP失效,请求不成功")

    def search_kw(self):
        self.browser.find_by_xpath('//input[@id="twotabsearchtextbox"]').first.fill(self.keyword)
        self.browser.find_by_xpath('//input[@value="Go"]').click()
        draggble = self.browser.find_by_xpath('//*[@id="searchTemplate"]')
        target = self.browser.find_by_xpath('//*[@id="footer"]')
        draggble.drag_and_drop(target)
        # time.sleep(random.randint(1, 6))

    def register(self):
        self.browser.find_by_xpath('//*[@id="nav-link-accountList"]').click()
        self.browser.find_by_xpath('//*[@id="createAccountSubmit"]').click()
        self.browser.find_by_xpath('//input[@id="ap_customer_name"]').first.fill(self.name)
        self.browser.find_by_xpath('//input[@id="ap_email"]').first.fill(self.email)
        self.browser.find_by_xpath('//input[@id="ap_password"]').first.fill(self.password)
        self.browser.find_by_xpath('//input[@id="ap_password_check"]').first.fill(self.password)
        self.browser.find_by_xpath('//input[@class="a-button-input"]').click()

    def add_pay_method(self):
        self.browser.find_by_xpath('//*[@id="nav-link-accountList"]').click()
        self.browser.find_by_xpath('//*[@id="a-page"]/div[3]/div/div[2]/div[2]/a/div').click()
        self.browser.fill('ppw-accountHolderName', paras['card_name'])
        self.browser.fill('addCreditCardNumber', paras['card_num'])
        self.browser.execute_script('document.getElementsByName("ppw-expirationDate_month")[0].style.display="block"')
        self.browser.execute_script('document.getElementsByName("ppw-expirationDate_year")[0].style.display="block"')
        self.browser.find_by_text('04').click()
        self.browser.find_by_text('2019').click()
        self.browser.find_by_name('ppw-widgetEvent:AddCreditCardEvent').click()

    def add_address(self):
        self.browser.fill('ppw-fullName', paras['fullname'])
        self.browser.fill('ppw-line1', paras['line'])
        self.browser.fill('ppw-city', paras['city'])
        self.browser.fill('ppw-stateOrRegion', paras['stateOrRegion'])
        self.browser.fill('ppw-postalCode', paras['postalCode'])
        self.browser.fill('ppw-phoneNumber', paras['phoneNumber'])
        self.browser.find_by_name('ppw-widgetEvent:AddAddressEvent').click()

    def buy_goods(self):
        self.browser.find_by_xpath('//*[@id="nav-recently-viewed"]').click()
        self.browser.find_by_xpath('//*[@id="asin_list"]/div[1]/div/a/div[1]/span/div').click()
        try:
            self.browser.find_by_xpath('//*[@id="add-to-cart-button"]').click()  # add cart
            self.browser.find_by_xpath('//*[@id="smartShelfAddToCartNative"]').click()
        except Exception as e:
            logging.error(e)
        try:
            time.sleep(5)
            self.browser.is_element_present_by_xpath('//i[@class="a-icon a-icon-close"]', wait_time=30)
            self.browser.find_by_xpath('//i[@class="a-icon a-icon-close"]').click()
        except Exception:
            pass
        try:
            self.browser.find_by_xpath('//a[@id="hlb-view-cart-announce"]').click()  # enter cart
        except Exception:
            pass
        self.browser.execute_script('document.getElementsByName("quantity")[0].style.display="block"')
        self.browser.find_by_value('10').click()
        self.browser.find_by_name('quantityBox').clear()
        self.browser.fill('quantityBox', 999)
        self.browser.find_by_xpath('//span[@id="a-autoid-1"]').click()
        self.browser.find_by_xpath('//div[@class="sc-proceed-to-checkout"]').click()
        self.browser.find_by_xpath('//*[@id="address-book-entry-0"]/div[2]/span/a').click()
        self.browser.find_by_xpath('//*[@id="shippingOptionFormId"]/div[3]/div/div/span[1]').click()
        self.browser.find_by_xpath('//*[@id="order-summary-container"]/div/div/div').click()
        try:
            self.browser.find_by_xpath('//a[contains(@class, "prime-nothanks-button")]').click()
        except Exception:
            pass
        self.browser.find_by_xpath('//span[contains(@class, "place-order-button-link")]').click()

    def add_list(self):
        self.browser.find_by_xpath('//*[@id="nav-recently-viewed"]').click()
        self.browser.find_by_xpath('//*[@id="asin_list"]/div[1]/div/a/div[1]/span/div').click()
        self.browser.find_by_xpath('//input[@title="Add to List"]').click()
        if self.browser.is_element_present_by_text('Add to your list', wait_time=10):
            self.browser.find_by_xpath('//*[@id="WLHUC_result"]/form/div[2]/span[3]/span').click()
            self.browser.find_by_xpath('//i[@class="a-icon a-icon-close"]').click()
        time.sleep(2)
        self.browser.quit()
示例#8
0
#!usr/bin/python
from splinter.browser import Browser
import re
import time
import sys
import os

SongName=sys.argv[1]
br=Browser('chrome')
br.driver.set_window_size(10,10)
br.visit("http://www.youtube.com")
time.sleep(5)
searchbar=br.find_by_id("masthead-search-terms").first.find_by_tag("input")[0]
searchbar.fill(SongName)
br.execute_script("if (document.getElementById('masthead-search-term').value == '') return false; document.getElementById('masthead-search').submit(); return false;;return true;")
URLfirst=br.find_by_id("results").first.find_by_tag("a")
YoutubeURL=URLfirst[1]['href']
br.quit()
os.system("cd ~/Downloads")
executionstring="youtube-dl -x --extract-audio --audio-format mp3 --audio-quality 0 "+YoutubeURL
os.system(executionstring)

##For Youtube-mp3.org method, uncomment all the lines below. Since I had
##youtube-dl installed on my laptop, I am using a method which has higher quality, but takes
##more time to install
#br.visit("http://www.youtube-mp3.org/")
#URLarea=br.find_by_id("youtube-url").fill(YoutubeURL)
#button=br.find_by_id("submit")
#button.click()
#print "Ready to click"
#time.sleep(10)
示例#9
0
class WeiboSpider(object):
    page_count = 0  # 记录每个页面抓取的数据量
    all_count = 0  # 记录抓取的数据总量
    save_data = OrderedDict({  # 保存抓取的数据
        "昵称": [],
        "微博正文": [],
        "微博链接": [],
        "时间": [],
        "收藏数": [],
        "转发数": [],
        "评论数": [],
        "点赞数": [],
        "设备": []
    })

    xpath_dict = {  # 解析数据用的 xpath
        '昵称': '//div[@class="info"]//a[@class="name"]/text()',
        '微博正文':
        '//div[@class="content"]/p[@node-type="feed_list_content"]//text()',
        '微博链接': '//div[@class="content"]/p[@class="from"]/a[1]/@href',
        '时间': '//div[@class="content"]/p[@class="from"]/a[1]/text()',
        '收藏数': '//a[@action-type="feed_list_favorite"]/text()',
        '转发数': '//a[@action-type="feed_list_forward"]/text()',
        '评论数': '//a[@action-type="feed_list_comment"]/text()',
        '点赞数':
        '//div[@class="card-act"]//a[@action-type="feed_list_like"]//em/text()',
        '设备': '//div[@class="content"]/p/a[@rel="nofollow"]/text()'
    }

    def __init__(self,
                 keyword,
                 start_time,
                 end_time,
                 sleep_time=10,
                 username=None,
                 password=None):
        self.username = username  # 微博用户名
        self.password = password  # 微博密码

        self.browser = None
        self.browser_name = "firefox"  # 浏览器名
        self.driver_path = "../driver/firefoxdriver.exe"  # 打开浏览器的驱动

        self.base_url = "https://s.weibo.com/"  # 微博搜索主页
        self.search_url = 'https://s.weibo.com/weibo/{keyword}' \
                          '&timescope=custom:{start_time}:{end_time}&refer=g'  # 搜索结果的url
        self.keyword = keyword  # 搜索关键字
        self.sleep_time = sleep_time  # 点击下一页的时间间隔
        self.start_time = start_time  # 搜索内容的起始时间
        self.end_time = end_time  # 搜索内容的结束时间

        self.base_sava_path = '../files/'  # 输出文件的保存路径
        self.save_file_name = self.keyword + self.start_time + "~" + self.end_time  # 输出文件名

    def refactor_date(self, start_time, end_time):  # 构造搜索的起止时间
        self.start_time = datetime.strptime(
            start_time, "%Y-%m-%d-%H").strftime("%Y-%m-%d-%H")
        if end_time:
            self.end_time = datetime.strptime(
                end_time, "%Y-%m-%d-%H").strftime("%Y-%m-%d-%H")
        else:
            self.end_time = datetime.now().strftime('%Y-%m-%d-%H')

    def get_search_url(self):  # 构造搜索url
        self.refactor_date(self.start_time, self.end_time)  # 将输入的时间重构成搜索用的标准参数

        return self.search_url.format(keyword=self.keyword,
                                      start_time=self.start_time,
                                      end_time=self.end_time)

    def login(self):
        """
        用于登录微博
        """
        self.browser = Browser(driver_name=self.browser_name,
                               executable_path=self.driver_path,
                               service_log_path='../files/log.log')  # 打开浏览器

        self.browser.visit(self.base_url)  # 访问微博搜索页面
        self.browser.click_link_by_text('登录')  # 点击登录

        # 填充用户名的密码
        if self.username is not None:
            self.browser.fill("username", self.username)
        if self.password is not None:
            self.browser.fill("password", self.password)

        print("请在打开的浏览器中登录........")

        time.sleep(2)  # 暂停两秒,等待浏览器加载完成
        logining_url = self.browser.url  # 获取正在登录时的 url

        # 防止网络不好时获取不到正在登录时的 url
        while logining_url == self.base_url:
            time.sleep(2)
            logining_url = self.browser.url

        # 通过验证 url 保证已经登录
        while 1:
            if self.browser.url != logining_url:
                break
            time.sleep(2)
        print("已成功登录,开始抓取信息.......")

    def search(self):
        """
        通过构造的搜索 url,跳转到搜索结果页面
        """

        self.browser.visit(self.get_search_url())

    def get_card_data(self, card, xapth_dict: dict):
        """
        用户获取每一篇博客的信息
        """

        etree_html = etree.HTML(card.html)
        number = ['收藏数', '转发数', '评论数', '点赞数']

        self.page_count += 1  # 统计每一页抓取的数据量

        for key in xapth_dict.keys():
            xpath = xapth_dict.get(key)
            data = etree_html.xpath(xpath)

            if data:
                if key in number:
                    self.save_data[key].append(Utils.get_num(data[0]))

                elif key == '时间':
                    self.save_data[key].append(
                        Utils.get_date(data[0]).strftime('%Y-%m-%d'))

                elif key == '微博正文':
                    content = ''.join(data).replace(' ', '').replace('\n', '')
                    self.save_data[key].append(content)

                else:
                    self.save_data[key].append(data[0])

            else:
                if key in number:
                    self.save_data[key].append('0')

                else:
                    self.save_data[key].append('')

    def download_data(self):
        self.browser.execute_script(
            "window.scrollTo(0,document.body.scrollHeight)")  # 跳转到页面底部

        try:
            self.browser.click_link_by_text('查看全部搜索结果')
        except ElementDoesNotExist:
            pass

        page_index = 1  # 记录页码
        while page_index <= 50:  # 微博搜索结果最多为50页

            # 获取真实的页码
            try:
                # 微博搜索结果有时候一直点击下一页,到了最后一页会跳转到第一页
                # 这段代码用于防止出现这种情况
                real_page = re.findall(r'page=(\d+)', self.browser.url)
                if real_page:
                    real_page = int(real_page[0])
                else:
                    real_page = 1
                if real_page < page_index:
                    break

                print('正在抓取第%s页内容:' % page_index, end='')
                self.browser.execute_script(
                    "window.scrollTo(0,document.body.scrollHeight)")  # 跳转到页面底部

                cards = self.browser.find_by_xpath(
                    '//div[@class="card"]')  # 获取所有的博文

            except KeyboardInterrupt:  # 如果觉得抓取时间过长,可以按下ctrl c 中止抓取,然后可以保存已抓取的信息
                print('中途退出抓取,正则保存中.....')
                break

            for card in cards:  # 遍历所有的文章,获取数据
                try:
                    self.get_card_data(card, self.xpath_dict)
                except KeyboardInterrupt:
                    pass

            try:
                print('本页抓取了%s条数据,模拟等待中.....' % self.page_count)
                self.all_count += self.page_count  # 统计获取的所有数据量
                self.page_count = 0

                # 基于设置的休眠时间,随机设置一个休眠值
                sleep_time = random.randint(self.sleep_time,
                                            self.sleep_time + 5)

                time.sleep(sleep_time)  # 模拟用户浏览网页的时间

                try:
                    self.browser.click_link_by_text('下一页')  # 点击下一页
                    page_index += 1

                except ElementDoesNotExist:
                    break

            except KeyboardInterrupt:
                print('中途退出抓取,正则保存中.....')
                break

    def save(self):
        """
        保存数据
        """

        print('--------------------------------------------')
        print('本次共抓取了%s条数据' % self.all_count)

        try:
            data = pandas.DataFrame(self.save_data)
            file_path = self.base_sava_path + self.save_file_name + '.xlsx'

            data.to_excel(file_path, index=False)  # 将数据保存到 excel
            print('文件正在保存...', end='\n\n')

        except Exception as e:
            print('文件保存失败!!!', end='\n\n')
            print(e)

    def close(self):
        self.browser.quit()  # 关闭浏览器

    @staticmethod
    def test():
        """
        用于测试的方法,实际运行时不执行
        """

        browser = Browser(executable_path="../driver/firefox.exe")
        browser.visit(
            "https://s.weibo.com/weibo/%25E5%25B0%25B1%25E5%25"
            "BC%2580%25E5%25A7%258B%25E5%25A4%25A7%25E5%25B9%2585?topnav=1&wvr=6&b=1"
        )

        cards = browser.find_by_xpath('//div[@class="card"]')
        for c in cards:
            etree_html = etree.HTML(c.html)
            a = etree_html.xpath(
                '//div[@class="card-act"]//a[@title="赞"]/em/text()')

            print(a[0])
示例#10
0
def check():
    global b
    b = Browser(driver_name="chrome")
    b.visit(ticket_url)
    b.execute_script('alert("开始刷票")')
    sleep(2)
    b.get_alert().dismiss()
    
    while b.is_text_present(u"登录"):
        sleep(1)
        login()
        if b.url == initmy_url:
            break
      
    try:
        # 跳回购票页面
        b.visit(ticket_url)
        
        # 加载车票查询信息
        b.cookies.add({"_jc_save_fromStation":starts})
        b.cookies.add({"_jc_save_toStation":ends})
        b.cookies.add({"_jc_save_fromDate":dtime})
        b.reload()
        i = 1        
        
        # 循环点击预订
        if order != 0:
            while b.url == ticket_url:
                sleep(3)
                b.find_by_text(u"查询").click()
#                b.find_by_text(ttype).click()
                
                if b.find_by_text(u"预订"):
                    sleep(0.3)
                    b.find_by_text(u"预订")[order - 1].click()
                    print(b.url)
                    
                    if b.is_text_present(u"证件号码",wait_time = 0.5):
#                        print [ i.text for i in b.find_by_text(pa) ]
                        b.find_by_text(pa)[1].click()
                        
                else:
                    b.execute_script('alert("没有可预订选项")')
                    b.get_alert().dismiss()
                    pass
                 
        else:
            while b.url == ticket_url:
                sleep(3)
                b.find_by_text(u"查询").click()
                if b.find_by_text(u"预订"):
                    sleep(0.3)
                    for i in b.find_by_text(u"预订"):                 
                        i.click()
                        sleep(0.1)
                        if b.is_text_present(u"证件号码"):
                            b.find_by_text(pa)[1].click()
                                                        
                    else:
                        b.execute_script('alert("似乎没有可预订选项")')
                        b.get_alert().dismiss()
                        pass
                     
        b.execute_script('alert("能做的都做了")')
        b.get_alert().dismiss()
        
        print(u"能做的都做了.....不再对浏览器进行任何操作")
        
    except Exception:
        print(u"出错了....")

SongName=raw_input("enter song ")
br=Browser('chrome')

br.driver.set_window_size(10,10)

br.visit("http://www.youtube.com")

time.sleep(5)

searchbar=br.find_by_id("masthead-search-terms").first.find_by_tag("input")[0]

searchbar.fill(SongName)

br.execute_script("document.getElementById('masthead-search').submit();")

URLfirst=br.find_by_id("results").first.find_by_tag("a")

for i in range(1,50):
	print i

	if (URLfirst[i]['href'].find("watch")!=-1 and len(URLfirst[i]['href'])< 50):
		YoutubeURL=URLfirst[i]['href']
		break


br.quit()

mp3 = pafy.new(YoutubeURL)
bestaudio = mp3.getbestaudio()
示例#12
0
    for channel in channels:
        # create output file
        rawfile = open("data/{}_raw.txt".format(channel), "w+")
        parsedfile = open("data/{}_parsed".format(channel), "w+")
        browser.visit("https://open.rocket.chat/channel/{}".format(channel))
        time.sleep(10)

        # scroll up to load previous messages
        # i have not figured out a way to detect when the channel is completely loaded
        # mostly because i am lazy and it doesnt really matter
        # so i just put 300 as a safe number
        # if it's too low, increase it. if it's too high (takes too long) decrease it
        # in any case it only takes 15 minutes to do 300 scrolls
        for i in range(300):
            browser.execute_script(
                "document.querySelector('.messages-box .wrapper').scrollTo(0, -document.querySelector('.messages-box .wrapper').scrollHeight);"
            )
            # for scroll loading time because rocket chat is a slow ass piece of shit
            time.sleep(3)

        # scrape raw file
        wrapper = browser.find_by_css('.messages-box .wrapper')
        html = BeautifulSoup(wrapper.html, 'html.parser')
        # use .replace('\\n', ' ').replace('\\t', '').replace('\\', '') to parse this
        rawfile.write(str(html.text.encode('unicode-escape')))

        # scrape parsed data
        data = []
        wrapper = browser.find_by_css('.messages-box .wrapper .message')
        i = 0
        while True:
示例#13
0
def huoche():
    global b
    b = Browser(driver_name="chrome")
    b.visit(ticket_url)

    while b.is_text_present(u"登录"):
        sleep(1)
        login()
        if b.url == initmy_url:
            break

    try:
        print u"进入购票页面..."
        # 跳回购票页面
        b.visit(ticket_url)

        # 加载查询信息
        b.cookies.add({"_jc_save_fromStation": starts})
        b.cookies.add({"_jc_save_toStation": ends})
        b.cookies.add({"_jc_save_fromDate": dtime})
        b.reload()
        b.cookies.all()

        sleep(2)

        count = 0
        # 循环点击预订
        if order != 0:
            while b.url == ticket_url:
                b.find_by_text(u"查询").click()
                count += 1
                print u"1循环点击查询... 第 %s 次, order=%d" % (count, order)
                sleep(10)  #in seconds
                try:
                    b.find_by_text(u"预订")[order - 1].click()
                    sleep(1)
                    if b.is_text_present(u"证件号码", wait_time=0.5):
                        b.find_by_text(pa)[1].click()
                        b.execute_script("alert('请选择验证码并提交订单')")
                        break
                except:
                    print u"还没开始预订"
                    continue
        else:
            while b.url == ticket_url:
                b.find_by_text(u"查询").click()
                count += 1
                #print u"循环点击查询... 第 %s 次" % count
                print u"2循环点击查询... 第 %s 次, order=%d" % (count, order)
                sleep(10)
                try:
                    for i in b.find_by_text(u"预订"):
                        i.click()
                        sleep(1)
                        if b.is_text_present(u"证件号码"):
                            b.find_by_text(pa)[1].click()
                            b.execute_script("alert('请选择验证码并提交订单')")
                            raise finishreservation()
                            #break;
                except finishreservation:
                    break
                except Exception as e:
                    print u"还没开始预订"
                    continue

        #sleep(1)
        #b.find_by_text(pa)[1].click()

        print u"请手动操作...."
    except Exception as e:
        print(traceback.print_exc())
In this example we will:
- send code to be executed on browser
- manipulate the DOM with javascript code with splinter
- get the data from javascript
NOTE: You will see the example code more advanced and organized in:
javascript_advanced_and_organized_code.py
"""
from splinter.browser import Browser

browser = Browser('chrome')
# Visit URL
url = "http://splinter.cobrateam.info/"
browser.visit(url)
# 1 - Send code to webpage to be executed
# you can execute a simple javascript code
browser.execute_script('alert("Hello World")')
# assign variable
javascript_code = 'message = "After this alert, the body tag will disappear";'
javascript_code += 'alert(message);'
browser.execute_script(javascript_code)
# you just can execute any javascript code, like,
# for example, manupulate dom
# javascript snippet to hidden the body
browser.execute_script('document.getElementsByTagName("body")[0].style.display = "none"')
# 2 - Getting data returned from javascript code execution
# getting value of the 'display' attribute from the body tag
value_of_display = browser.evaluate_script('document.getElementsByTagName("body")[0].style.display')
browser.execute_script('alert("As you could see, the body disappeared, so the display is set as: %s");' % value_of_display)
browser.execute_script('alert("..but now");')
# back to the normal, that is, display:block
browser.execute_script('document.getElementsByTagName("body")[0].style.display = "block"')
示例#15
0
#!usr/bin/python
from splinter.browser import Browser
import re
import time

br=Browser()
br.visit("http://www.160by2.com/Index")
br.fill('username','YOUR_PHONE_NUMBER_USED_TO_CREATE_160BY2_ACCOUNT')
br.fill('password','YOUR_ACCOUNT_PASSWORD')
button = br.find_by_name("")
button.click()

stringurl=br.url
stringurl2=stringurl.split("id=",1)[1]
id=stringurl2.split("id=",1)[1]
jsstring="window.parent.openPage('SendSMS?id="+id+"', 'aSendSMS', 'aSMS', 'ulSMS')"
br.execute_script(jsstring)
time.sleep(8)
br.execute_script(jsstring)
print "JS Executed"
time.sleep(8)

with br.get_iframe('by2Frame') as iframe:
	iframe.fill("sendSMSMsg","Well, I guess robot's do send SMSes when spandan wants them to ;)")
	iframe.find_by_tag("input")[10].fill("THE_PHONE_NUMBER_YOU_WANT_TO_SEND_SMS_TO")
	button2=iframe.find_by_id("btnsendsms").first
	print button2.value
	button2.click()
示例#16
0
from splinter.browser import Browser
import re
import time

br = Browser()
br.visit("http://www.160by2.com/Index")
br.fill('username', 'YOUR_PHONE_NUMBER_USED_TO_CREATE_160BY2_ACCOUNT')
br.fill('password', 'YOUR_ACCOUNT_PASSWORD')
button = br.find_by_name("")
button.click()

stringurl = br.url
stringurl2 = stringurl.split("id=", 1)[1]
id = stringurl2.split("id=", 1)[1]
jsstring = "window.parent.openPage('SendSMS?id=" + id + "', 'aSendSMS', 'aSMS', 'ulSMS')"
br.execute_script(jsstring)
time.sleep(8)
br.execute_script(jsstring)
print "JS Executed"
time.sleep(8)

with br.get_iframe('by2Frame') as iframe:
    iframe.fill(
        "sendSMSMsg",
        "Well, I guess robot's do send SMSes when spandan wants them to ;)")
    iframe.find_by_tag("input")[10].fill(
        "THE_PHONE_NUMBER_YOU_WANT_TO_SEND_SMS_TO")
    button2 = iframe.find_by_id("btnsendsms").first
    print button2.value
    button2.click()
示例#17
0
文件: a.py 项目: ueu2715/python
def huoche():
    global b
    b = Browser(driver_name="chrome")
    b.visit(ticket_url)

    while b.is_text_present(u"登录"):
        sleep(1)
        login()
        if b.url == initmy_url:
           break

    try:
        print (u"购票页面")
        # 跳回购票页面
        b.visit(ticket_url)

        # 加载查询信息
        b.cookies.add({"_jc_save_fromStation": starts})
        b.cookies.add({"_jc_save_toStation": ends})
        b.cookies.add({"_jc_save_fromDate": dtime})
        b.reload()

        

        b.find_by_text(u"更多选项").click()
        sleep(1)
        b.find_by_id("inp-train").fill(tnum)
        b.find_by_id("add-train").click()
        #b.find_by_text(u"K-快速").click()
        b.find_by_text(u"请选择")[1].click()
        b.find_by_text(seatcn)[1].click()
        b.execute_script("$.closeSelectSeat()")
        sleep(2)

        count = 0
        flag = ""
        # 循环点击预订
        print ("order is %s" % order)
        if order != 0:
            
            while b.url == ticket_url:
                b.find_by_text(u"查询").click()
                count +=1
                print (u"循环点击查询... 第 %s 次" % count)
                sleep(5)
                try:
                    b.find_by_text(u"预订")[order - 1].click()
                except:
                    print (u"还没开始预订")
                    continue
        else:
            while b.url == ticket_url:
                b.find_by_text(u"查询").click()
                count += 1
                #flag = b.find_by_id("YW_240000K11711")[0].text
                
                                
               
                
                #print ("ticket count is %s" % flag)
                sleep(2)
                #b.execute_script("$('a:contains("+tnum+")').closest('tr').children('td:eq("+seat+")').addClass('abcde')")
                #flag = b.find_by_xpath("//td[@class='abcde']").text
                flag = b.find_by_xpath("//td[contains(@id,'"+seat+"')][contains(@id,'"+tnum+"')]").text
                print (u"循环点击查询... 第 %s 次 tickets count is %s" % (count,flag))
                try:
                    #for i in b.find_by_text(u"预订"):
                    #    i.click()
                    if flag != "--" and flag != u"无":
                        b.execute_script("$('a:contains("+tnum+")').closest('tr').children('td:last').children().click()")
                        break
                except:
                    print (u"还没开始预订")
                    continue
                sleep(3)

        pat = pa.split(",")
        
        while True:
            try:
                flag = True
                for p in pat:
                    if p == usernamecn:
                        if b.find_by_text(p)[1].checked != True:
                            flag = False
                            b.find_by_text(p)[1].check()
                            flag = True
                    else:
                        if b.find_by_text(p)[0].checked != True:
                            flag = False
                            b.find_by_text(p)[0].check()
                            flag = True
                    print (u"选择乘客:%s" % p)
                if flag:
                    break
            except:
                print (u"努力选中陛下的乘客信息中~~~")
            sleep(0.5)
        print ( u"能做的都做了.....不再对浏览器进行任何操作")
    except Exception as e:
        print(traceback.print_exc())