最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 獲取頁面表格數(shù)據(jù)存放到csv中的方法

 更新時間:2018年12月26日 15:16:02   作者:云中不知人  
今天小編就為大家分享一篇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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

兴山县| 三门峡市| 连平县| 普陀区| 连州市| 达州市| 芒康县| 大同市| 永清县| 藁城市| 专栏| 左云县| 隆化县| 吉林省| 大庆市| 根河市| 城市| 禄劝| 绥中县| 玉龙| 措美县| 永胜县| 湘阴县| 自贡市| 巴青县| 英山县| 兴文县| 余姚市| 中超| 湄潭县| 金华市| 浑源县| 毕节市| 响水县| 会宁县| 天全县| 彰武县| 苍溪县| 阳新县| 嘉祥县| 绍兴市|