首先,嘗試使用.querySelectorAll()方法在瀏覽器控制臺上選擇第一個員工的名字。這個方法的一個真正有用的特性是,可以越來越深入地實現(xiàn)大于(>)符號的層次結(jié)構(gòu),以定義父元素(在左側(cè))和要獲取的子元素(在右側(cè))。
元素,這些元素就會成為節(jié)點列表。因為不能依賴類來獲取每個單元格,所以只需要知道它們在索引中的位置,而第一個name是0。 從那里,可以像這樣編寫代碼: for row in rows: name = row.find_all('td')[0].text print(name) 簡單地說,逐個獲取每一行,并找到其中的所有單元格,一旦有了列表,只獲取索引中的第一個單元格(position 0),然后使用.text方法只獲取元素的文本,忽略不需要的HTML數(shù)據(jù)。 這是一個包含所有員工姓名的列表! 對于其余部分,只需要遵循同樣的邏輯: position = row.find_all('td')[1].text office = row.find_all('td')[2].text age = row.find_all('td')[3].text start_date = row.find_all('td')[4].text salary = row.find_all('td')[5].text然而,將所有這些數(shù)據(jù)輸出在控制臺上并沒有太大幫助。與其相反,可以將這些數(shù)據(jù)存儲為一種、更有用的新格式。 4.將表格數(shù)據(jù)存儲到JSON文件中 雖然可以輕松地創(chuàng)建一個CSV文件并將數(shù)據(jù)發(fā)送到那里,但如果可以使用抓取的數(shù)據(jù)創(chuàng)建一些新內(nèi)容,那么這將不是最容易管理的格式。 盡管如此,以前做的一個項目解釋了如何創(chuàng)建一個CSV文件來存儲抓取的數(shù)據(jù)。 好消息是,Python有自己的JSON模塊來處理JSON對象,所以不需要安裝任何程序,只需要導(dǎo)入它。 import json 但是,在繼續(xù)并創(chuàng)建JSON文件之前,需要將所有這些抓取的數(shù)據(jù)轉(zhuǎn)換為一個列表。為此,將在循環(huán)外部創(chuàng)建一個空數(shù)組。 employee_list = [] 然后向它追加數(shù)據(jù),每個循環(huán)向數(shù)組追加一個新對象。 employee_list.append({ 'Name': name, 'Position': position, 'Office': office, 'Age': age, 'Start date': start_date, 'salary': salary })如果print(employee_list),其結(jié)果如下: Employee_List 還是有點混亂,但已經(jīng)有了一組準備轉(zhuǎn)換為JSON的對象。 注:作為測試,輸出employee_list的長度,它返回57,這是抓取的正確行數(shù)(行現(xiàn)在是數(shù)組中的對象)。 將列表導(dǎo)入到JSON只需要兩行代碼: with open('json_data', 'w') as json_file: json.dump(employee_list, json_file, indent=2)
- 首先,打開一個新文件,傳入想要的文件名稱(json_data)和'w',因為想要寫入數(shù)據(jù)。
- 接下來,使用.dump()函數(shù)從數(shù)組(employee_list)和indent=2中轉(zhuǎn)儲數(shù)據(jù),這樣每個對象都有自己的行,而不是所有內(nèi)容都在一個不可讀的行中。
5.運行腳本和完整代碼 如果一直按照下面的方法做,那么代碼庫應(yīng)該是這樣的: #dependencies import requests from bs4 import BeautifulSoup import json url = 'http://api.scraperapi.com?api_key=51e43be283e4db2a5afbxxxxxxxxxxx&url=https://datatables.net/examples/styling/stripe.html' #empty array employee_list = [] #requesting and parsing the HTML file response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') #selecting the table table = soup.find('table', class_ = 'stripe') #storing all rows into one variable for employee_data in table.find_all('tbody'): rows = employee_data.find_all('tr') #looping through the HTML table to scrape the data for row in rows: name = row.find_all('td')[0].text position = row.find_all('td')[1].text office = row.find_all('td')[2].text age = row.find_all('td')[3].text start_date = row.find_all('td')[4].text salary = row.find_all('td')[5].text #sending scraped data to the empty array employee_list.append({ 'Name': name, 'Position': position, 'Office': office, 'Age': age, 'Start date': start_date, 'salary': salary }) #importing the array to a JSON file with open('employee_data', 'w') as json_file: json.dump(employee_list, json_file, indent=2) 注:在這里為場景添加了一些注釋。 以下是JSON文件中的前三個對象: 以JSON格式存儲抓取數(shù)據(jù)允將信息用于新的應(yīng)用程序 使用Pandas抓取HTML表 在離開頁面之前,希望探索第二種抓取HTML表的方法。只需幾行代碼,就可以從HTML文檔中抓取所有表格數(shù)據(jù),并使用Pandas將其存儲到數(shù)據(jù)框架中。 在項目的目錄中創(chuàng)建一個新文件夾(將其命名為panda-html-table-scraper),并創(chuàng)建一個新文件名pandas_table_scraper.py。 打開一個新的終端,導(dǎo)航到剛剛創(chuàng)建的文件夾(cdpanda-html-table-scraper),并從那里安裝pandas: pip install pandas 在文件的頂部導(dǎo)入它。 import pandas as pd Pandas有一個名為read_html()的函數(shù),它主要抓取目標URL,并返回所有HTML表作為DataFrame對象的列表。 要實現(xiàn)這一點,HTML表至少需要結(jié)構(gòu)化,因為該函數(shù)將查找 |