Requirements: Remove the contents of the basket box and write to the CSV file. Each row is taken out as an element in a new array with two for loops.
1 #Coding=utf-82 fromSeleniumImportWebdriver3 fromTimeImportSleep4 Importkeyword5 fromSelenium.webdriver.common.keysImportKeys6 fromSelenium.webdriver.support.waitImportwebdriverwait7 fromSelenium.webdriver.support.uiImportSelect8 ImportCSV9 ImportRandomTen Importio OneOutputfilepath ="F:\CaipiaoOutputData.csv" A #Scroll scroll bar ' - defMovescorllbar (driver,down,range1,range2): -Monecount=Random.randint (Range1, Range2) the forNuminchRange (1, Monecount): - if(down==" Down"): -Driver.find_element_by_xpath ("//body"). Send_keys (Keys.down) -Sleep (Random.uniform (0.1,0.5)) + Else: -Driver.find_element_by_xpath ("//body"). Send_keys (keys.up) +Sleep (Random.uniform (0.1,0.5)) A return at #writes the read content to a new CSV document - defSavecsvfile (filePath): -CsvFile2 = open (FilePath,'W', newline="')#set newline, otherwise there will be a blank line between the two lines -writer =Csv.writer (csvFile2) -Writer.writerow (['Events','Start time','hosts','Visiting Team','Data','Home wins','Draw','The visiting team wins','Betting Distribution']) - writer.writerows (outputtr) in #csvfile.close () - #Main function to if __name__=="__main__": +Outputtr=[] -Driver =Webdriver. Chrome () theDriver.get ("http://caipiao.hao123.com/") *Movescorllbar (Driver," Down", 30, 60) $ " "Panax Notoginseng ths = driver.find_element_by_id ("Panel-zucai-jingji"). Find_elements_by_tag_name ("th") - For th in ths: the print (Th.text) + outputtr.append (Th.text)" " ATRS = Driver.find_element_by_xpath ('//*[@id = "Panel-zucai-jingji"]/div/table'). Find_elements_by_tag_name ("TR") the forTrinchTRS: +OutputString = [] -Tds=tr.find_elements_by_tag_name ("TD") $ forTdinchTDs: $ outputstring.append (Td.text) - Print(outputstring) - outputtr.append (outputstring) the - Print(outputtr)WuyiSavecsvfile (Outputfilepath)
"Python" Two for loop nesting exercises