欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3中for循環(huán)踩過的坑記錄

 更新時(shí)間:2020年12月14日 15:00:36   作者:低調(diào)的城主  
這篇文章主要給大家介紹了python3中for循環(huán)踩坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

最近在用python練習(xí)寫點(diǎn)爬蟲,想著把雙色球的歷史記錄爬下來存入mysql中,爬取數(shù)據(jù)沒有遇到什么問題,在處理數(shù)據(jù)存入數(shù)據(jù)庫(kù)的時(shí)候遇到問題了,現(xiàn)把問題整理出來方便自己日后查詢也能幫助有緣人士:

一、從雙色球歷史網(wǎng)站爬取數(shù)據(jù)存成html文件;

import urllib.request
 
url = 'https://datachart.500.com/ssq/history/newinc/history.php?start=1&end=20109'
request = urllib.request.Request(url)
request.add_header('user-agent',
     'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36')
response = urllib.request.urlopen(request)
buf = response.read()
data = buf.decode('utf-8')
# 爬取數(shù)據(jù)保存到文件
fileOb = open('history.html', 'w', encoding='utf-8') # 打開一個(gè)文件,沒有就新建一個(gè)
fileOb.write(data)
fileOb.close()

二,讀取html文件獲取數(shù)據(jù),其中還有mysql的連接池

from lxml import etree
from collections import namedtuple
import mysql2
 
fileOb = open('history.html', 'r', encoding='utf-8') # 打開一個(gè)文件
doc = fileOb.read()
html = etree.HTML(doc) # 把字符串轉(zhuǎn)化為可處理的格式
two_colour = namedtuple('two_colour', 'code,red_1,red_2,red_3,red_4,red_5,red_6,blue,create_date')
 
 
# two_colour = namedtuple('two_colour', 'a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16')
 
 
def test1():
 # red = html.xpath("http://tbody/tr/td[@class='t_cfont2']/text()[1]")
 # blue = html.xpath("http://tbody/tr/td[@class='t_cfont4']/text()[1]")
 all_ball = html.xpath("http://tbody/tr[@class='t_tr1']/td/text()")
 # print(all_ball)
 for i in range(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])
  print(dict)
  for j in range(16):
   del all_ball[0]
  mysql2.saveDouBan(dict)
 print(all_ball)
 
 
if __name__ == '__main__':
 test1()

mysql的連接池

import pymysql
# 新的寫法,要注意
from dbutils.pooled_db import PooledDB
 
POOL = PooledDB(
 creator=pymysql, # 使用鏈接數(shù)據(jù)庫(kù)的模塊
 maxconnections=6, # 連接池允許的最大連接數(shù),0和None表示不限制連接數(shù)
 mincached=2, # 初始化時(shí),鏈接池中至少創(chuàng)建的空閑的鏈接,0表示不創(chuàng)建
 maxcached=5, # 鏈接池中最多閑置的鏈接,0和None不限制
 maxshared=3,
 # 鏈接池中最多共享的鏈接數(shù)量,0和None表示全部共享。PS: 無用,因?yàn)閜ymysql和MySQLdb等模塊的 threadsafety都為1,所有值無論設(shè)置為多少,_maxcached永遠(yuǎn)為0,所以永遠(yuǎn)是所有鏈接都共享。
 blocking=True, # 連接池中如果沒有可用連接后,是否阻塞等待。True,等待;False,不等待然后報(bào)錯(cuò)
 maxusage=None, # 一個(gè)鏈接最多被重復(fù)使用的次數(shù),None表示無限制
 setsession=[], # 開始會(huì)話前執(zhí)行的命令列表。
 ping=0,
 # ping MySQL服務(wù)端,檢查是否服務(wù)可用。
 host='127.0.0.0',
 port=3306,
 user='root',
 password='123456',
 database='python_test',
 charset='utf8'
)
 
 
def func():
 # 檢測(cè)當(dāng)前正在運(yùn)行連接數(shù)的是否小于最大鏈接數(shù),如果不小于則:等待或報(bào)raise TooManyConnections異常
 # 否則 則優(yōu)先去初始化時(shí)創(chuàng)建的鏈接中獲取鏈接 SteadyDBConnection。
 # 然后將SteadyDBConnection對(duì)象封裝到PooledDedicatedDBConnection中并返回。
 # 如果最開始創(chuàng)建的鏈接沒有鏈接,則去創(chuàng)建一個(gè)SteadyDBConnection對(duì)象,再封裝到PooledDedicatedDBConnection中并返回。
 # 一旦關(guān)閉鏈接后,連接就返回到連接池讓后續(xù)線程繼續(xù)使用。
 conn = POOL.connection()
 
 # print(th, '鏈接被拿走了', conn1._con)
 # print(th, '池子里目前有', pool._idle_cache, '\r\n')
 
 cursor = conn.cursor()
 cursor.execute('select * from two_clour_two')
 result = cursor.fetchall()
 for i in result:
  print(i)
 conn.close()
 
 
# 數(shù)據(jù)庫(kù)插入操作
def saveDouBan(dict):
 conn = POOL.connection()
 cursor = conn.cursor()
 sql = "insert into two_clour_two (`code`, `red_1`, `red_2`, `red_3`, `red_4`, `red_5`, `red_6`, `blue`,`create_date`) values(\"%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]),
  str(dict[7]), str(dict[8]))
 cursor.execute(sql)
 print('Successful')
 conn.commit() # 寫入數(shù)據(jù)庫(kù)一定要commit,否則數(shù)據(jù)沒有數(shù)據(jù)
 cursor.close()
 
 
if __name__ == '__main__':
 func()

三,數(shù)據(jù)處理的誤區(qū),這個(gè)是剛開始的寫法,i是下標(biāo),取完數(shù)據(jù)把對(duì)應(yīng)的下標(biāo)的元素刪除了,可以這時(shí)候的問題就是,下標(biāo)的數(shù)字比數(shù)組長(zhǎng)了,所有最后下標(biāo)取完了,可是數(shù)組卻沒有為空。

解決辦法就是上面貼的

for i in range(len(all_ball)):

總結(jié)

到此這篇關(guān)于python3中for循環(huán)踩坑記錄的文章就介紹到這了,更多相關(guān)python3 for循環(huán)踩坑內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論