python多线程的使用

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
import random
import _thread
totalnum=0 #总行数
currentNum=[0,0,0,0,0,0,0,0] #d当前行数
totalcount=[0]
def isuser(driver,name):
    isu = True
    email = driver.find_element_by_id("ap_email")
    if email is not None:
        email.click()
        email.clear()
        email.send_keys(name)
        continue_bu = driver.find_element_by_id('continue')  # 找到导出按钮
        continue_bu.click()
        try:
            result = driver.find_element_by_id("signInSubmit")
        except NoSuchElementException:
            print(name + " 不是亚马逊用户")
            return False
    print(name + " 是亚马逊用户")
    driver.get(
        url='https://www.amazon.co.jp/ap/signin?_encoding=UTF8&openid.assoc_handle=jpflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_sel'
            'ect&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.n'
            'et%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2F'
            'www.amazon.co.jp%2Fgp%2Fcart%2Fview.html%3Fie%3DUTF8%26'
            'app-nav-type%3Dnone%26dc%3Ddf%26dc%3Ddf%26path%3D%252Fgp%252Fcart%252Fview.html%253Fapp-nav-type%253Dnone%26ref_%3Dcart_empty_sign_in%26useRedire'
            'ctOnSuccess%3D1')

    return isu


def main(filename,f,cur,thread):
    chrome_options = webdriver.ChromeOptions()

    chrome_driver = r"C:\Users\59886\Desktop\邮箱采集工具\chromedriver.exe"  #指定driver
    # chrome_driver = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver,options=chrome_options)
    driver.get(url='https://www.amazon.co.jp/ap/signin?_encoding=UTF8&openid.assoc_handle=jpflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_sel'
                   'ect&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.n'
                   'et%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2F'
                   'www.amazon.co.jp%2Fgp%2Fcart%2Fview.html%3Fie%3DUTF8%26'
                   'app-nav-type%3Dnone%26dc%3Ddf%26dc%3Ddf%26path%3D%252Fgp%252Fcart%252Fview.html%253Fapp-nav-type%253Dnone%26ref_%3Dcart_empty_sign_in%26useRedire'
                   'ctOnSuccess%3D1')
    while(cur[thread-1]<totalnum//8*thread):
        name = f[cur[thread-1]].strip("\n")
        time.sleep(1 + random.randint(1, 3))
        if isuser(driver, name):
            with open(filename+'--user.txt','a') as f1:
                f1.write(name + '\n')
        cur[thread-1]+=1
        totalcount[0]+=1
    return cur

filename=input("请输入文件名:")
# f1=open('user.txt','a')

f=open(filename).readlines()
totalnum=len(f)
currentNum=[0,totalnum//8,totalnum//4,totalnum//8*3,totalnum//2,totalnum//8*5,totalnum//8*6,totalnum//8*7]


# 创建两个线程
try:
    _thread.start_new_thread(main, (filename,f, currentNum, 1))
    _thread.start_new_thread(main, (filename,f, currentNum, 2))
    _thread.start_new_thread(main, (filename,f, currentNum, 3))
    _thread.start_new_thread(main, (filename,f, currentNum, 4))
    _thread.start_new_thread(main, (filename,f, currentNum, 5))
    _thread.start_new_thread(main, (filename,f, currentNum, 6))
    _thread.start_new_thread(main, (filename,f, currentNum, 7))
    _thread.start_new_thread(main, (filename,f, currentNum, 8))


except:
   print ("Error: 无法启动线程")

while totalcount[0]<totalnum:
   pass

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,多线程是一种并发编程技术,允许一个程序同时执行多个任务,每个任务在单独的线程中运行。使用Python的内置`threading`模块可以轻松地创建和管理线程。以下是使用Python线程的基本步骤: 1. 导入`threading`模块:这是开始多线程编程的第一步。 ```python import threading ``` 2. 定义线程函数(target):你需要为每个线程定义一个函数,这个函数将在新线程中执行。 ```python def worker_function(arg): # 线程执行的代码 pass ``` 3. 创建线程对象:使用`Thread`类实例化一个线程,将定义的函数作为参数传递。 ```python thread = threading.Thread(target=worker_function, args=(arg,)) ``` 4. 启动线程:调用`start()`方法启动线程,此时线程会在后台运行。 ```python thread.start() ``` 5. 等待线程完成:如果你需要等待线程结束,可以使用`join()`方法。 ```python thread.join() # 如果你想让主线程等待这个线程结束 ``` 6. 错误处理:线程可能遇到异常,使用`threading`模块提供的异常处理器来捕获并处理。 ```python try: thread.start() except Exception as e: print(f"Error in thread: {e}") ``` 需要注意的是,Python中的全局解释器锁(GIL)限制了同一时间只能有一个线程在执行Python字节码。这意味着多线程并不能真正实现并行计算,但在IO密集型任务中仍然很有用,比如网络请求、文件读写等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值