Skip to content

Instantly share code, notes, and snippets.

@IanSmith123
Last active January 22, 2019 14:24
Show Gist options
  • Select an option

  • Save IanSmith123/b89a5b642d66a696c0ef89c90d8e067d to your computer and use it in GitHub Desktop.

Select an option

Save IanSmith123/b89a5b642d66a696c0ef89c90d8e067d to your computer and use it in GitHub Desktop.

Revisions

  1. IanSmith123 revised this gist Jan 22, 2019. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions v2ex_catch_fish.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import os
    import sys
    import json
    import argparse
    from time import sleep
    from random import randint, shuffle
    from selenium import webdriver
    @@ -9,7 +10,7 @@
    driver_dumpfile = 'cookie.txt'

    try:
    target_score = sys.argv[1]
    target_score = int(sys.argv[1])
    except IndexError:
    target_score = 2018

    @@ -77,7 +78,7 @@ def water(d: webdriver.Chrome, user, target):
    for url in urls:
    count += 1
    d.get(url)
    wait_time = randint(3, 25)
    wait_time = randint(3, 15)
    print("第{}次刷新, 等待{}s".format(count, wait_time))
    sleep(wait_time)

    @@ -115,8 +116,8 @@ def check_dau(d: webdriver.Chrome, user):


    if __name__ == "__main__":
    username = "asdfasdf"
    password = "asdfasdf"
    username = "e"
    password = "A"
    # 如果文件不存在,则重新登录
    driver = webdriver.Chrome()
    driver.get(site_url)
  2. IanSmith123 revised this gist Dec 31, 2018. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions v2ex_catch_fish.py
    Original file line number Diff line number Diff line change
    @@ -103,20 +103,20 @@ def check_dau(d: webdriver.Chrome, user):
    order = int(order)

    current_dau = 0
    if order < 2:
    if order < 11:
    # 位于前十
    d.get('https://www.v2ex.com/top/dau')
    dau_list = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div[2]/table/tbody/tr/td[3]/div').text
    dau_list = [int(item) for item in dau_list]
    dau_list = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div[2]/table/tbody/tr/td[3]/div')
    dau_list = [int(item.text) for item in dau_list]
    print(dau_list)
    current_dau = dau_list[order - 1]

    return order, current_dau


    if __name__ == "__main__":
    username = "asdf"
    password = "asdf"
    username = "asdfasdf"
    password = "asdfasdf"
    # 如果文件不存在,则重新登录
    driver = webdriver.Chrome()
    driver.get(site_url)
  3. IanSmith123 revised this gist Dec 31, 2018. 1 changed file with 72 additions and 12 deletions.
    84 changes: 72 additions & 12 deletions v2ex_catch_fish.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,38 @@
    from selenium import webdriver
    import os
    import sys
    import json
    from time import sleep
    from random import randint, shuffle
    from selenium import webdriver

    site_url = "https://www.v2ex.com"
    driver_dumpfile = 'cookie.txt'

    try:
    target_score = sys.argv[1]
    except IndexError:
    target_score = 2018


    def dump_cookie(d: webdriver.Chrome, dumpfile):
    with open(dumpfile, 'w', encoding='utf8') as f:
    cookies = json.dumps(d.get_cookies(), indent=2)
    f.write(cookies)
    # f.write(d.get_cookies())


    def load_cookie(d: webdriver.Chrome, dumpfile: str) -> webdriver.Chrome:
    with open(dumpfile, 'r', encoding='utf8') as f:
    cookies = json.loads(f.read())

    for cookie in cookies:
    # print(cookie)
    d.add_cookie(cookie)
    print("读取cookie完成")
    return d

    def login(d: webdriver.Chrome, user, passwd) -> webdriver.Chrome:

    def login(d: webdriver.Chrome, user, passwd, dumpfile) -> webdriver.Chrome:
    # d.get("https://www.v2ex.com")
    # sleep(3)
    d.get("https://www.v2ex.com/signin")
    @@ -17,10 +46,21 @@ def login(d: webdriver.Chrome, user, passwd) -> webdriver.Chrome:
    # 10s内手动输入验证码,然后回车
    sleep(10)

    # 检查是否登录成功
    if user == d.find_element_by_xpath('//*[@id="Top"]/div/div/table/tbody/tr/td[3]/a[2]').text:
    print("login success")
    print(d.get_cookies())
    dump_cookie(d, dumpfile)

    else:
    print("login failed")
    exit(1)

    return d


    def water(d: webdriver.Chrome, user):
    def water(d: webdriver.Chrome, user, target):
    d.get(site_url)
    urls = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div/table/tbody/tr/td[3]/span[1]/a')
    # print(urls)
    urls = [item.get_attribute("href") for item in urls]
    @@ -44,27 +84,47 @@ def water(d: webdriver.Chrome, user):
    # 检查dau
    seed -= 1
    if not seed:
    dau = check_dau(d, user)
    print("当前活跃度排名 {}".format(dau))
    order, current_dau = check_dau(d, user)
    print("当前活跃度排名 {}, 分数 {}".format(order, current_dau))
    sleep(randint(5, 9))
    seed = randint(4, 15) # 更新seed
    if target - current_dau < 20:
    print("积分接近 {}, 自动退出".format(target))
    exit(0)

    if count > 1000:
    break


    return count


    def check_dau(d: webdriver.Chrome, user):
    d.get('https://www.v2ex.com/member/{}'.format(user))
    order = d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[1]/table/tbody/tr/td[3]/span[2]/a').text
    return order
    order = int(order)

    current_dau = 0
    if order < 2:
    # 位于前十
    d.get('https://www.v2ex.com/top/dau')
    dau_list = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div[2]/table/tbody/tr/td[3]/div').text
    dau_list = [int(item) for item in dau_list]
    print(dau_list)
    current_dau = dau_list[order - 1]

    return order, current_dau


    if __name__ == "__main__":
    username = "a"
    password = "a"
    username = "asdf"
    password = "asdf"
    # 如果文件不存在,则重新登录
    driver = webdriver.Chrome()
    driver = login(d=driver, user=username, passwd=password)
    fish = water(d=driver, user=username)
    driver.get(site_url)
    sleep(1)
    if os.path.isfile(driver_dumpfile) and os.path.getsize(driver_dumpfile):
    driver = load_cookie(d=driver, dumpfile=driver_dumpfile)
    else:
    driver = login(d=driver, user=username, passwd=password, dumpfile=driver_dumpfile)

    sleep(randint(1, 4))
    fish = water(d=driver, user=username, target=target_score)
  4. IanSmith123 revised this gist Dec 30, 2018. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions v2ex_catch_fish.py
    Original file line number Diff line number Diff line change
    @@ -28,26 +28,31 @@ def water(d: webdriver.Chrome, user):
    if len(urls) < 1:
    print("error")
    return "error"
    count = 0

    # 开始划水
    count = 0
    seed = randint(1, 4) # 预生成一个seed
    while True:
    shuffle(urls)
    seed = randint(5, len(urls)) # 生成用于检测dau的随机数
    for url in urls:
    count += 1
    d.get(url)
    wait_time = randint(3, 25)
    print("第{}次刷新, 等待{}s".format(count, wait_time))
    sleep(wait_time)

    if not count % seed:
    # 检查dau
    seed -= 1
    if not seed:
    dau = check_dau(d, user)
    print("当前活跃度排名 {}".format(dau))
    sleep(randint(5, 9))
    seed = randint(4, 15) # 更新seed

    if count > 1000:
    break


    return count


    @@ -58,8 +63,8 @@ def check_dau(d: webdriver.Chrome, user):


    if __name__ == "__main__":
    username = "username"
    password = "password"
    username = "a"
    password = "a"
    driver = webdriver.Chrome()
    driver = login(d=driver, user=username, passwd=password)
    fish = water(d=driver, user=username)
  5. IanSmith123 revised this gist Dec 30, 2018. 1 changed file with 24 additions and 12 deletions.
    36 changes: 24 additions & 12 deletions v2ex_catch_fish.py
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,26 @@
    from selenium import webdriver
    from time import sleep
    from random import randint
    from random import randint, shuffle


    def login(d: webdriver.Chrome, user, passwd) -> webdriver.Chrome:
    # d.get("https://www.v2ex.com")
    # sleep(3)
    d.get("https://www.v2ex.com/signin")
    #输入账号和密码
    # 输入账号和密码
    d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[1]/td[2]/input').send_keys(user)
    d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[2]/td[2]/input').send_keys(passwd)
    # 定位到验证码输入框
    d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[3]/td[2]/input').click()
    print("在网页中输入验证码")

    # 10s内手动输入验证码,然后回车
    sleep(10)

    return d


    def water(d: webdriver.Chrome):
    def water(d: webdriver.Chrome, user):
    urls = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div/table/tbody/tr/td[3]/span[1]/a')
    # print(urls)
    urls = [item.get_attribute("href") for item in urls]
    @@ -28,26 +31,35 @@ def water(d: webdriver.Chrome):
    count = 0
    # 开始划水
    while True:
    shuffle(urls)
    seed = randint(5, len(urls)) # 生成用于检测dau的随机数
    for url in urls:
    count += 1
    d.get(url)
    wait_time = randint(3,25)
    wait_time = randint(3, 25)
    print("第{}次刷新, 等待{}s".format(count, wait_time))
    sleep(wait_time)

    if count > 50:
    sleep(20)
    if count > 100:
    if not count % seed:
    dau = check_dau(d, user)
    print("当前活跃度排名 {}".format(dau))
    sleep(randint(5, 9))

    if count > 1000:
    break

    # d.minimize_window()
    print(count)
    return count


    def check_dau(d: webdriver.Chrome, user):
    d.get('https://www.v2ex.com/member/{}'.format(user))
    order = d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[1]/table/tbody/tr/td[3]/span[2]/a').text
    return order


    if __name__ == "__main__":
    username = "USEGF"
    password = "wAFJWFAifa"
    username = "username"
    password = "password"
    driver = webdriver.Chrome()
    driver = login(d=driver, user=username, passwd=password)
    fish = water(d=driver)
    fish = water(d=driver, user=username)
  6. IanSmith123 revised this gist Dec 28, 2018. No changes.
  7. IanSmith123 created this gist Dec 28, 2018.
    53 changes: 53 additions & 0 deletions v2ex_catch_fish.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    from selenium import webdriver
    from time import sleep
    from random import randint


    def login(d: webdriver.Chrome, user, passwd) -> webdriver.Chrome:
    # d.get("https://www.v2ex.com")
    # sleep(3)
    d.get("https://www.v2ex.com/signin")
    #输入账号和密码
    d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[1]/td[2]/input').send_keys(user)
    d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[2]/td[2]/input').send_keys(passwd)

    # 10s内手动输入验证码,然后回车
    sleep(10)

    return d


    def water(d: webdriver.Chrome):
    urls = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div/table/tbody/tr/td[3]/span[1]/a')
    # print(urls)
    urls = [item.get_attribute("href") for item in urls]
    print(urls)
    if len(urls) < 1:
    print("error")
    return "error"
    count = 0
    # 开始划水
    while True:
    for url in urls:
    count += 1
    d.get(url)
    wait_time = randint(3,25)
    print("第{}次刷新, 等待{}s".format(count, wait_time))
    sleep(wait_time)

    if count > 50:
    sleep(20)
    if count > 100:
    break

    # d.minimize_window()
    print(count)
    return count


    if __name__ == "__main__":
    username = "USEGF"
    password = "wAFJWFAifa"
    driver = webdriver.Chrome()
    driver = login(d=driver, user=username, passwd=password)
    fish = water(d=driver)