Python两个for循环嵌套练习
Posted 猪猪宝丫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python两个for循环嵌套练习相关的知识,希望对你有一定的参考价值。
要求:取下方篮框中内容并写入csv文件中。用两个for循环将每一行取出来作为一个元素存放到新数组中。
1 # coding=utf-8 2 from selenium import webdriver 3 from time import sleep 4 import keyword 5 from selenium.webdriver.common.keys import Keys 6 from selenium.webdriver.support.wait import WebDriverWait 7 from selenium.webdriver.support.ui import Select 8 import csv 9 import random 10 import io 11 outputFilePath = "F:\\CaipiaoOutputData.csv" 12 #滚动滚动条` 13 def MoveScorllBar(driver,down,range1,range2): 14 moneCount=random.randint(range1, range2) 15 for num in range(1,moneCount): 16 if(down=="down"): 17 driver.find_element_by_xpath("//body").send_keys(Keys.DOWN) 18 sleep(random.uniform(0.1,0.5)) 19 else: 20 driver.find_element_by_xpath("//body").send_keys(Keys.UP) 21 sleep(random.uniform(0.1,0.5)) 22 return 23 #将读取的内容写入一个新的csv文档 24 def Savecsvfile(filePath): 25 csvFile2 = open(filePath, \'w\', newline=\'\') # 设置newline,否则两行之间会空一行 26 writer = csv.writer(csvFile2) 27 writer.writerow([\'赛事\', \'开赛时间\', \'主队\', \'客队\', \'数据\', \'主队胜\', \'平局\', \'客队胜\', \'投注分布\']) 28 writer.writerows(outputTr) 29 #csvfile.close() 30 #主函数 31 if __name__=="__main__": 32 outputTr=[] 33 driver = webdriver.Chrome() 34 driver.get("http://caipiao.hao123.com/") 35 MoveScorllBar(driver, "down", 30, 60) 36 \'\'\' 37 ths = driver.find_element_by_id("panel-zucai-jingji").find_elements_by_tag_name("th") 38 for th in ths: 39 print(th.text) 40 outputTr.append(th.text)\'\'\' 41 trs = driver.find_element_by_xpath(\'//*[@id="panel-zucai-jingji"]/div/table\').find_elements_by_tag_name("tr") 42 for tr in trs: 43 outputString = [] 44 tds=tr.find_elements_by_tag_name("td") 45 for td in tds: 46 outputString.append(td.text) 47 print(outputString) 48 outputTr.append(outputString) 49 50 print(outputTr) 51 Savecsvfile(outputFilePath)
以上是关于Python两个for循环嵌套练习的主要内容,如果未能解决你的问题,请参考以下文章