⑴ 为什么用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