python 獲取頁面表格數(shù)據(jù)存放到csv中的方法
獲取單獨一個table,代碼如下:
#!/usr/bin/env python3
# _*_ coding=utf-8 _*_
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.request import HTTPError
try:
html = urlopen("http://en.wikipedia.org/wiki/Comparison_of_text_editors")
except HTTPError as e:
print("not found")
bsObj = BeautifulSoup(html,"html.parser")
table = bsObj.findAll("table",{"class":"wikitable"})[0]
if table is None:
print("no table");
exit(1)
rows = table.findAll("tr")
csvFile = open("editors.csv",'wt',newline='',encoding='utf-8')
writer = csv.writer(csvFile)
try:
for row in rows:
csvRow = []
for cell in row.findAll(['td','th']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
finally:
csvFile.close()
獲取所有table,代碼如下:
#!/usr/bin/env python3
# _*_ coding=utf-8 _*_
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.request import HTTPError
try:
html = urlopen("http://en.wikipedia.org/wiki/Comparison_of_text_editors")
except HTTPError as e:
print("not found")
bsObj = BeautifulSoup(html,"html.parser")
tables = bsObj.findAll("table",{"class":"wikitable"})
if tables is None:
print("no table");
exit(1)
i = 1
for table in tables:
fileName = "table%s.csv" % i
rows = table.findAll("tr")
csvFile = open(fileName,'wt',newline='',encoding='utf-8')
writer = csv.writer(csvFile)
try:
for row in rows:
csvRow = []
for cell in row.findAll(['td','th']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
finally:
csvFile.close()
i += 1
以上這篇python 獲取頁面表格數(shù)據(jù)存放到csv中的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- python 刪除excel表格重復行,數(shù)據(jù)預處理操作
- Python3讀取和寫入excel表格數(shù)據(jù)的示例代碼
- 基于Python快速處理PDF表格數(shù)據(jù)
- Python基于pandas爬取網(wǎng)頁表格數(shù)據(jù)
- 基于python實現(xiàn)把json數(shù)據(jù)轉(zhuǎn)換成Excel表格
- 使用 Python 讀取電子表格中的數(shù)據(jù)實例詳解
- python讀取word 中指定位置的表格及表格數(shù)據(jù)
- python 中Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格
- Python 用三行代碼提取PDF表格數(shù)據(jù)
- Python獲取數(shù)據(jù)庫數(shù)據(jù)并保存在excel表格中的方法
- python3 讀取Excel表格中的數(shù)據(jù)
- 利用python做表格數(shù)據(jù)處理
相關文章
在python tkinter中Canvas實現(xiàn)進度條顯示的方法
今天小編就為大家分享一篇在python tkinter中Canvas實現(xiàn)進度條顯示的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python?基于xml.etree.ElementTree實現(xiàn)XML對比示例詳解
這篇文章主要介紹了Python?基于xml.etree.ElementTree實現(xiàn)XML對比,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Pandas中map(),applymap(),apply()函數(shù)的使用方法
本文主要介紹了Pandas中map(),applymap(),apply()函數(shù)的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
詳述numpy中的np.random.random()系列函數(shù)用法
本文主要介紹了詳述numpy中的np.random.random()系列函數(shù)用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03
利用Pycharm斷點調(diào)試Python程序的方法
今天小編就為大家分享一篇利用Pycharm斷點調(diào)試Python程序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
日常整理python執(zhí)行系統(tǒng)命令的常見方法(全)
本文是小編日常整理的些關于python執(zhí)行系統(tǒng)命令常見的方法,比較全面,特此通過腳本之家這個平臺把此篇文章分享給大家供大家參考2015-10-10
python目標檢測yolo1?yolo2?yolo3和SSD網(wǎng)絡結(jié)構(gòu)對比
這篇文章主要為大家介紹了python目標檢測yolo1?yolo2?yolo3和SSD網(wǎng)絡結(jié)構(gòu)對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05

