Python3 practice-obtain data from a website and python3
As a newbie, you can view and practice some simple practical applications. The following program obtains the required data from a website.
The program may not write well, but it basically meets its own needs.
Hope you can give me some advice ~~
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 # the total number of pages is 39. You need to determine from the page first, or you can capture from the page, 13 link = urllink + str (n) 14 links can be improved in the future. 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 # when using requests directly, the Chinese content needs to be transcoded 23 24 soup = BeautifulSoup (rep. content, 'html. parser ') 25 26 # print (soup. pretloads () 27 ## pretloads () 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 is the total list of Daily Data 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)