⑴ 為什麼用python提取html不全
用python提取html不全的原因:
現在的網站上面有很多的反爬措施,最常見的就是json非同步載入,網頁上面的數據是json代碼載入出來的,所以爬取的html信息不全
具體示例如下:
瀏覽器顯示的內容
實際上爬蟲訪問鏈接得到的內容:
更多Python知識,請關註:Python自學網!!
⑵ python使用plotly生成了多個離線圖表,如何將他們合並成一個html做展示
本人在使用groovy爬取了全國3000+城市的歷史天氣之後,需要把每個城市的歷史天氣都繪制一張Time Series表格,用來反映各地的最高溫最低溫溫差的變化曲線。這里遇到了一個問題,每次plotly繪制完圖標總會調起系統瀏覽器打開呈現,一旦我批量生成N多張表格時,電腦就會卡死了。在使用中文作為文件名的時候遇到了一個錯誤,這個錯誤剛好能巧妙解決這個問題。在不同編碼格式的字元拼接時文件路徑時,會報錯,報錯內容如下:
'ascii' codec can't encode characters in position 69-70: ordinal not in range(128)
然後程序停止運行,但是文件已經生成了。在做了異常處理後,剛好能滿足需求。關於python2.7的編碼問題,並不是很了解為什麼出這個錯。有大神了解的可以分享一下。
python部分的代碼如下:
#!/usr/bin/python
# coding=utf-8
from first.date import DatePlot
import os
from second.MysqlFission import MysqlFission
import shutil
import time
class Fission:
x = []
y = []
z = []
d = []
def __init__(self):
print "歡迎使用fission類!"
# def __init__(self,x,y,z,d):
# def __init__(self,name):
# self.name = name
# print "歡迎使用fission類!"
def getData(self, name):
size = 0;
with open("/Users/Vicky/Documents/workspace/source_api/long/" + name + ".log") as apidata:
for i in apidata:
data = i.split(" ")[0].split("|")[0]
low = i.split(" ")[0].split("|")[1]
high = i.split(" ")[0].split("|")[2]
diff = int(high) - int(low)
self.x.append(data)
self.y.append(low)
self.z.append(high)
self.d.append(diff)
size += 1;
def getDataMarkLine(self, name):
with open("/Users/Vicky/Documents/workspace/source_api/long/" + name + ".log") as apidata:
for i in apidata:
data = i.split(" ")[0].split("|")
day = data[0]
time = float(data[1])
self.x.append(day)
self.y.append(time)
return [self.x, self.y]
if __name__ == "__main__":
names = []
for name in names:
name = u"三沙"
sql = MysqlFission()
sql.getWeather(name)
fission = Fission()
fission.x = []
fission.y = []
fission.z = []
fission.d = []
fission.getData(name)
try:
DatePlot.MakePlotTwo(fission.x, name, high=fission.y, low=fission.z, diff=fission.d)
except BaseException:
print 2
shutil.file(name + ".html", "/Users/Vicky/Desktop/w/" + name + ".html")
os.remove(name + ".html")
time.sleep(5)
下面是北京市的效果圖:
⑶ python 系列 05 - 基於plotly的數據可視化
本篇內容主要介紹如何使用Plotly庫進行數據可視化,包括安裝、圖表繪制、模塊功能、導出靜態圖像、定製選項以及生成離線圖表等。
直接選用Plotly庫進行可視化,安裝步驟略過。
使用一組虛擬降雨量數據,通過折線圖、柱狀圖、餅圖和散點圖展示數據的不同形式。
運行代碼將生成互動式html頁面,展示動態效果。
更多圖表類型通過相應代碼實現。
Plotly庫包含三個主要模塊,子包詳情可參考官網。
通過kaleido庫導出,安裝kaleido後,導出過程簡單便捷。
展示如何在一個頁面上展示不同類型圖表。
提供兩種方式,一是生成離線html文件,二是以dom節點嵌入到html中。
⑷ python怎樣做html的表格
現要實現python製作html格式的表格,利用Python對字元串str.format()格式化操作進行處理,在日常對CVS格式文件處理過程當中,經常會將CVS格式文件進行轉換,在正式場合是程序讀取CVS文件進行轉換並輸出到html格式的文件當中,但現在只是實現一下轉換的過程,需要輸入以逗號分隔的數據。
在設計程式的時候,需要先定義一下整個代碼的框架,首先我們要定義一個主函數main(),雖然Python沒有規定入口函數,一般在正式的開發中都設計了一個main()函數作為程序的入口函數,或許這是一種規范吧。然後我們在定義一個列印表頭的方法print_head(),並在主函數里進行調用。再定義一個列印表尾的方法print_end(),也在主函數中進行調用。定義print_line()為列印表格行,定義extract_field()處理cvs行數據轉換為list集合數據。最後再定義一個處理特殊符號的方法escape_html(),因為在html代碼中為了避免與它的標簽沖突,特要進行特殊符號的轉換,如&-->&
還有就是對長度過長的數據要進行處理並用...代替
源代碼:
#Author Tandaly
#Date 2013-04-09
#File Csv2html.py
#主函數
def main():
print_head()
maxWidth = 100
count = 0
while True:
try:
line = str(input())
if count == 0:
color = "lightgreen"
elif count%2 == 0:
color = "white"
else:
color = "lightyellow"
print_line(line, color, maxWidth)
count += 1
except EOFError:
break
print_end()
#列印表格頭
def print_head():
print("")
#列印錶行
def print_line(line, color, maxWidth):
tr = "".format(color)
tds = ""
if line is not None and len(line) > 0:
fields = axtract_fields(line)
for filed in fields:
td = "{0}".format(filed if (len(str(filed)) <= maxWidth) else
(str(filed)[:100] + "..."))
tds += td
tr += "{0}
".format(tds)
print(tr)
#列印表格尾
def print_end():
print("")
#抽取行值
def axtract_fields(line):
line = escape_html(line)
fields = []
field = ""
quote = None
for c in line:
if c in "\"":
if quote is None:
quote = c
elif quote == c:
quote = None
continue
if quote is not None:
field += c
continue
if c in ",":
fields.append(field)
field = ""
else:
field += c
if len(field) > 0:
fields.append(field)
return fields
#處理特殊符號
def escape_html(text):
text = text.replace("&", "&")
text = text.replace(">", ">")
text = text.replace("<", "<")
return text
#程序入口
if __name__ == "__main__":
main()
運行結果:
>>>
"nihao","wo"
nihaowo
"sss","tandaly"
...tandaly
"lkkkkkkkkkkksdfssssssssssssss",
34
...34