python 獲取頁(yè)面表格數(shù)據(jù)存放到csv中的方法
獲取單獨(dú)一個(gè)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 獲取頁(yè)面表格數(shù)據(jù)存放到csv中的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作
- Python3讀取和寫入excel表格數(shù)據(jù)的示例代碼
- 基于Python快速處理PDF表格數(shù)據(jù)
- Python基于pandas爬取網(wǎng)頁(yè)表格數(shù)據(jù)
- 基于python實(shí)現(xiàn)把json數(shù)據(jù)轉(zhuǎn)換成Excel表格
- 使用 Python 讀取電子表格中的數(shù)據(jù)實(shí)例詳解
- python讀取word 中指定位置的表格及表格數(shù)據(jù)
- python 中Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格
- Python 用三行代碼提取PDF表格數(shù)據(jù)
- Python獲取數(shù)據(jù)庫(kù)數(shù)據(jù)并保存在excel表格中的方法
- python3 讀取Excel表格中的數(shù)據(jù)
- 利用python做表格數(shù)據(jù)處理
相關(guān)文章
在python tkinter中Canvas實(shí)現(xiàn)進(jìn)度條顯示的方法
今天小編就為大家分享一篇在python tkinter中Canvas實(shí)現(xiàn)進(jìn)度條顯示的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
對(duì)python制作自己的數(shù)據(jù)集實(shí)例講解
今天小編就為大家分享一篇對(duì)python制作自己的數(shù)據(jù)集實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python?基于xml.etree.ElementTree實(shí)現(xiàn)XML對(duì)比示例詳解
這篇文章主要介紹了Python?基于xml.etree.ElementTree實(shí)現(xiàn)XML對(duì)比,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
Pandas中map(),applymap(),apply()函數(shù)的使用方法
本文主要介紹了Pandas中map(),applymap(),apply()函數(shù)的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
詳述numpy中的np.random.random()系列函數(shù)用法
本文主要介紹了詳述numpy中的np.random.random()系列函數(shù)用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
利用Pycharm斷點(diǎn)調(diào)試Python程序的方法
今天小編就為大家分享一篇利用Pycharm斷點(diǎn)調(diào)試Python程序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
日常整理python執(zhí)行系統(tǒng)命令的常見方法(全)
本文是小編日常整理的些關(guān)于python執(zhí)行系統(tǒng)命令常見的方法,比較全面,特此通過(guò)腳本之家這個(gè)平臺(tái)把此篇文章分享給大家供大家參考2015-10-10
python目標(biāo)檢測(cè)yolo1?yolo2?yolo3和SSD網(wǎng)絡(luò)結(jié)構(gòu)對(duì)比
這篇文章主要為大家介紹了python目標(biāo)檢測(cè)yolo1?yolo2?yolo3和SSD網(wǎng)絡(luò)結(jié)構(gòu)對(duì)比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

