在python3项目中使用的循环时会遇到哪些问题

  介绍

这期内容当中小编将会给大家带来有关在python3项目中使用的循环时会遇到哪些问题,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

<强>一,从双色球历史网站爬取数据存成html文件;

import  urllib.request   ,   时间=url  & # 39; https://datachart.500.com/ssq/history/newinc/history.php?start=1&结束=20109 & # 39;   时间=request  urllib.request.Request (url)   request.add_header(& # 39;用户代理# 39;   ,,,,& # 39;Mozilla/5.0, (Windows  NT  10.0;, Win64;, x64), AppleWebKit/537.36, (KHTML, like 壁虎),Chrome/86.0.4240.183  Safari/537.36 & # 39;)   时间=response  urllib.request.urlopen(请求)   时间=buf  response.read ()   时间=data  buf.decode (& # 39; utf - 8 # 39;)   #,爬取数据保存到文件   时间=fileOb 开放(& # 39;history.html& # 39;,, & # 39; w # 39;,,编码=& # 39;utf - 8 # 39;), #,打开一个文件,没有就新建一个   fileOb.write(数据)   fileOb.close ()

在python3项目中使用的循环时会遇到哪些问题

<强>二,读取html文件获取数据,其中还有mysql的连接池

得到lxml  import  etree   得到collections  import  namedtuple   import  mysql2   ,   时间=fileOb 开放(& # 39;history.html& # 39;,, & # 39; " # 39;,,编码=& # 39;utf - 8 # 39;), #,打开一个文件   时间=doc  fileOb.read ()   时间=html  etree.HTML (doc), #,把字符串转化为可处理的格式   时间=two_colour  namedtuple (& # 39; two_colour& # 39;,, & # 39;代码,red_1, red_2, red_3, red_4, red_5, red_6,蓝色,create_date& # 39;)   ,   ,=#,two_colour  namedtuple (& # 39; two_colour& # 39;,, & # 39; a1, a2, a3、a4、a5、a6、a7, a8, a9, a10, a11, a12, a13,阿,a15, a16 # 39;)   ,   ,   def  test1 ():   ,# red =, html.xpath(“//身体/tr/td (@class=& # 39; t_cfont2& # 39;]/text () [1]“)   ,# blue =, html.xpath(“//身体/tr/td (@class=& # 39; t_cfont4& # 39;]/text () [1]“)=,,all_ball  html.xpath(“//身体/tr (@class=& # 39; t_tr1& # 39;]/td/text ()“)   ,#打印(all_ball)   ,for 小姐:拷贝范围(len (all_ball)):   时间=dict 才能;two_colour (all_ball [0],, all_ball [1],, all_ball [2],, all_ball [3],, all_ball [4],, all_ball [5],, all_ball [6],   ,,,,,,all_ball [7],, all_ball [15])   打印(dict)才能   for 才能;j 拷贝范围(16):   ,,del  all_ball [0]   mysql2.saveDouBan才能(东西)   ,打印(all_ball)   ,   ,   if  __name__ ==, & # 39; __main__ # 39;:   之前,test1 ()

mysql的连接池

import  pymysql   #,新的写法,要注意   得到dbutils.pooled_db  import  PooledDB   ,   时间=POOL  PooledDB (   ,创造者=pymysql #,使用链接数据库的模块   ,maxconnections=6, #,连接池允许的最大连接数,0和没有表示不限制连接数   ,mincached=2, #,初始化时,链接池中至少创建的空闲的链接,0表示不创建   ,maxcached=5, #,链接池中最多闲置的链接,0和没有不限制   ,maxshared=3,   ,#链接池中最多共享的链接数量,0和没有表示全部共享每分钟:,无用,因为pymysql和MySQLdb等模块的,threadsafety都为1,所有值无论设置为多少,_maxcached永远为0,所以永远是所有链接都共享。   ,阻塞=True, #,连接池中如果没有可用连接后,是否阻塞等待.True,等待;假,不等待然后报的错   ,maxusage=没有#,一个链接最多被重复使用的次数,没有表示无限制   ,setsession=[], #,开始会话前执行的命令列表。   ,ping=0,   ,# ping  MySQL服务端,检查是否服务可用。   ,主机=& # 39;127.0.0.0& # 39;   ,端口=3306,   ,用户=& # 39;根# 39;   ,密码=& # 39;123456 & # 39;   ,数据库=& # 39;python_test& # 39;   ,charset=& # 39; use utf8 # 39;   )   ,   ,   def  func ():   ,#检测当前正在运行连接数的是否小于最大链接数,如果不小于则:等待或报raise  TooManyConnections异常   ,#否则,则优先去初始化时创建的链接中获取链接,SteadyDBConnection。   ,#,然后将SteadyDBConnection对象封装到PooledDedicatedDBConnection中并返回。   ,#如果最开始创建的链接没有链接,则去创建一个SteadyDBConnection对象,再封装到PooledDedicatedDBConnection中并返回。   ,#一旦关闭链接后,连接就返回到连接池让后续线程继续使用。=,,conn  POOL.connection ()   ,   ,#打印(th, & # 39;链接被拿走了& # 39;,,conn1._con)   ,#打印(th, & # 39;池子里目前有& # 39;,,pool._idle_cache,, & # 39; \ r \ n # 39;)   ,=,,cursor  conn.cursor ()   ,cursor.execute (& # 39; select  *,得到two_clour_two& # 39;)=,,result  cursor.fetchall ()   ,for 小姐:拷贝的结果:   打印(i)才能   ,conn.close ()   ,   ,   #,数据库插入操作   def  saveDouBan (dict类型):=,,conn  POOL.connection ()=,,cursor  conn.cursor ()=,sql “insert  into  two_clour_two (“代码”,“red_1”,,“red_2”,,“red_3”,,“red_4”,,“red_5”,,“red_6”,,“蓝”、“create_date”),值(\“% s \”, \“% s \”, \“% s \”, \“% s \”, \“% s \”, \“% s \”, \“% s \”, \“% s \”, \“% s \“)“, %, (   str才能(dict [0]), str (dict [1]), str (dict [2]), str (dict [3]), str (dict [4]), str (dict [5]), str (dict [6]),   null   null   null   null   null   null   null   null   null

在python3项目中使用的循环时会遇到哪些问题