python3實踐-從某網站擷取資料,python3擷取
作為一個新手,自己邊看邊實踐一些簡單的實際應用,下面的程式是從某個網站上擷取需要的資料。
程式寫的可能並不好,但基本上實現了自己的需求。
希望有高手來指點下~~
1 # Code based on Python 3.x 2 # _*_ coding: utf-8 _*_ 3 # __Author: "LEMON" 4 5 from bs4 import BeautifulSoup 6 import requests 7 import csv 8 9 urllink = 'http://www.bjets.com.cn/article/jyxx/?'10 links = []11 for n in range(2, 40):12 #頁面總數為39頁,需要自己先從網頁判斷,也可以從頁面抓取,後續可以完善13 link = urllink + str(n)14 links.append(link)15 links.insert(0, urllink)16 # print(links)17 18 for url in links:19 20 rep = requests.get(url)21 # content = rep.text.encode(rep.encoding).decode('utf-8')22 # # 直接用requests時,中文內容需要轉碼23 24 soup = BeautifulSoup(rep.content, 'html.parser')25 26 # print(soup.prettify())27 # # prettify()28 29 body = soup.body30 data = body.find('div', {'class': 'list_right'})31 32 # table title33 titles = data.find_all('th')34 35 title = []36 for x in titles:37 title.append(x.text)38 # print(title)39 40 quotes = data.find_all('tr')41 quotes1 = quotes[1:len(quotes)]42 # print(quotes1)43 44 list1 = []45 for x in quotes1:46 for y in x.find_all('td'):47 list1.append(y.text)48 # print(list1) # list為每日資料的總列表49 50 date = []51 volumes = []52 meanprice = []53 totalmoney = []54 55 for i in range(0, len(list1)):56 if i % 4 == 0:57 date.append(list1[i])58 elif i % 4 == 1:59 volumes.append(list1[i])60 elif i % 4 == 2:61 meanprice.append(list1[i])62 else:63 totalmoney.append(list1[i])64 65 # print(date)66 # print(volumes)67 # print(meanprice)68 # print(totalmoney)69 70 final = []71 for i in range(0, len(date)):72 temp = [date[i], volumes[i], meanprice[i], totalmoney[i]]73 final.append(temp)74 # print(final)75 with open('bj_carbon.csv', 'a', errors='ignore', newline='') as f:76 f_csv = csv.writer(f)77 f_csv.writerows(final)