当前位置:首页 » 编程语言 » pythonhtml

pythonhtml

发布时间: 2022-01-11 11:48:43

python怎么解析html文档

Python提供了一个HTMLParser模块,可以非常简单的解析HTML

首先考虑如何从如下的HTML中提取信息

<!--basic.html>
<HTML>

<HEAD>
<TITLE>DocTitle&ampIntro</TITLE>
</HEAD>

<BODY>
Thisismytext.
</BODY>

</HTML>
首先定义一个TitleParser类,是标准HTMLParser类的子孙
HTMLParser的feed()方法会调用handle_starttag(),handle_data(),handle_endtag()方法

#!/usr/bin/envpython
#coding=utf-8importsys

classTitleParser(HTMLParser):
def__init__(self):
self.title=''
self.readingtitle=0
HTMLParser.__init__(self)

defhandle_starttag(self,tag,attrs):
iftag=='title':
self.readingtitle=1

defhandle_data(self,data):
ifself.readingtitle:
self.title+=data

defhandle_endtag(self,tag):
iftag=='title':
self.readingtitle=0

defhandle_entityref(self,name):
ifentitydefs.has_key(name):
self.handle_data(entitydefs[name])
else:
self.handle_data('&'+name+';')

defgettitle(self):
returnself.title

fd=open(sys.argv[1])
tp=TitleParser()
tp.feed(fd.read())
print"Titleis:",tp.gettitle()

② python和html哪个好

Python和html的区别:

html是一种标记语言,是前端必须懂的一种语言之一。

而Python是作为后端语言的,所以你想学习前端只有一种选择,学html。

对于初学者来说,html和Python上手比较快。

Python目前广泛应用于人工智能,是AI的第一开发语言,有很多网站也是用Python写的。

单单只会html是不能胜任前端这个职位的,还需要学习css和JavaScript才能胜任。

所以学习前端的话html好,学习后端的话Python好。

更多Python知识,请关注:Python自学网!!

③ 如何将python代码嵌入html代码中

用Gestalt能够改变现状的Javascript库与Silverlight结合后,它可以让开发者直接在HTML中嵌入Python,在查看站点源代码时,会看到类似这样的代码:

<scriptlanguage="python">

将这句话包含在页面顶部,这就是在着手写Python前所要做的所有准备。它会将内联的Python代码传给Silverlight运行时,该运行时支持Dynamic Language Runtime。

④ python是否可以写网站使用python写的网站html在哪里

当然,html一般以模板的形式存放在不同的框架目录中。

⑤ 如何用Python做HTML的页面

<html>
<body>
<form>
可获取码列表:
<select name="liscode">
<option value="01">123456</option>
<option value="02">123457</option>
<option value="03">123458</option>
<option value="04">123459</option>
<option value="05">123460</option>
<option value="06">123461</option>
</select>
<input type="submit" value="确认获取"/>
</form>
</body>
</html>
其中所有liscode是从一个txt文档上提取的,当用户点击获取一个的时候,该项即被删除。
如何用python实现呢?
做一个py脚本或exe给用户实现的话大概像下面这样:
Python code
infile = open('codelist.txt','r') codelist = infile.readlines() used_code = codelist[0] #remove用掉的code(删除行) codelist.remove(codelist[0]) infile.close() #重写文件(我不知道是否有能直接删除一行的文件操作方法) outfile = open('codelist.txt','w') for code in codelist: outfile.write(code + '\n') outfile.close() print used_code

⑥ 怎样用Python写一个Html的简单网页

1、打开sublime text 3,新建一个PY文件。

⑦ 怎样用python脚本生成一个html格式的测试报告

比如很简单的,可以这样:
# -*- coding:utf-8 -*-
import os,sys

html = open('index.html', 'w')
html.write("""
<html>
<head>
<title>Test</title>
<style>img{float:left;margin:5px;}</style>
</head>
<body>
""")

files = os.listdir('.')

# 首先处理文本
for f in files:
if f.lower().endswith('.txt'):
fp = open(f)
content = fp.read()
fp.close()
html.write("<p>%s</p>" % content)

# 然后处理图片
for f in files:
if f.lower().endswith('.jpg') or f.lower().endswith('.png'):
html.write("<img src='%s' />" % f)

html.write('</body></html>')
html.close()

把这个python代码放在有图片和txt文本的目录里,运行就可以了。如果不是jpg,修改增加png,gif就行了。

⑧ python HTML处理

[<tdstyle="text-align:center">2014-12-31</td>,<tdstyle="text-align:center">6.16</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>1.76</td>]
[<tdstyle="text-align:center">2013-12-31</td>,<tdstyle="text-align:center">11.47</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>1.72</td>]
[<tdstyle="text-align:center">2012-12-31</td>,<tdstyle="text-align:center">-5.5</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>4.31</td>]
[<tdstyle="text-align:center">2011-12-31</td>,<tdstyle="text-align:center">14.83</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>0.53</td>]
[<tdstyle="text-align:center">2010-12-31</td>,<tdstyle="text-align:center">1.8</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>0.63</td>]
[<tdstyle="text-align:center">2009-12-31</td>,<tdstyle="text-align:center">21.69</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>5.61</td>]
[<tdstyle="text-align:center">2008-12-31</td>,<tdstyle="text-align:center">0.19</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>0.03</td>]
[<tdstyle="text-align:center">2007-12-31</td>,<tdstyle="text-align:center">2.89</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>0.77</td>]
[<tdstyle="text-align:center">2006-12-31</td>,<tdstyle="text-align:center">7.37</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>2.71</td>]
[<tdstyle="text-align:center">2005-12-31</td>,<tdstyle="text-align:center">3.99</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>28.66</td>]
[<tdstyle="text-align:center">2004-12-31</td>,<tdstyle="text-align:center">-132.62</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>78.84</td>]
[<tdstyle="text-align:center">2003-12-31</td>,<tdstyle="text-align:center">23.79</td>,<tdstyle="text-align:center">听</td>]
[<tdstyle="text-align:center">2002-12-31</td>,<tdstyle="text-align:center">-759.2139</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>745.9739</td>]
[<tdstyle="text-align:center">2001-12-31</td>,<tdstyle="text-align:center">-5.47</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>6.852</td>]
[<tdstyle="text-align:center">2000-12-31</td>,<tdstyle="text-align:center">6.82</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>3.95</td>]
[<tdstyle="text-align:center">1999-12-31</td>,<tdstyle="text-align:center">10.48</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>6.87</td>]
[<tdstyle="text-align:center">1998-12-31</td>,<tdstyle="text-align:center">12.28</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>7.21</td>]
[<tdstyle="text-align:center">1997-12-31</td>,<tdstyle="text-align:center">54</td>,<tdstyle="text-align:center"><fontstyle="color:green">隆媒</font>18</td>]
[<tdstyle="text-align:center">1996-12-31</td>,<tdstyle="text-align:center">72</td>,<tdstyle="text-align:center"><fontstyle="color:red">隆眉</font>7</td>]
[<tdstyle="text-align:center">1995-12-31</td>,<tdstyle="text-align:center">65</td>,<tdstyle="text-align:center">听</td>]

不止2004年的数据

我修改了下你的代码用的requests的库

importos
importsys
importtime
importrequests
importbs4
frompprintimportpprint

roe=open('ROE.txt','w')

req="http://vip.stock.finance.sina.com.cn/corp/view/vFD_FinancialGuideLineHistory.php?stockid=600159&typecode=financialratios59"
soup=bs4.BeautifulSoup(requests.get(req,timeout=30).text)
buffer=soup.find(id='Table1')
rows=buffer.find_all('tr')
forrowinrows:
cols=row.find_all('td')
text=str(cols)
if"-12-31"intext:
roe.write(text+' ')

time.sleep(2)

roe.close()

⑨ python 怎么提取html内容啊(正则)

python提取html内容的方法。如下参考:

1.首先,打开Python来定义字符串,在定义的字符串后面加上中括号,然后在要提取的字符位置输入。

⑩ 我打算用python去处理html的form表单,该怎么实现

运用 web.py 框架
例如 index页面有两个输入框
<form action='/index',method='post'>
<input type="text" name="name" id="name" />
<input type="text" name="pwd" id="pwd" />
</form>
那么 在python 中
class index:
def GET(self, name):
inputall =web.input(name=None,pwd=None)
name= inputall.name
pwd= inputall.pwd
print print name ,pwd
def POST(self, name):
inputall =web.input(name=None,pwd=None)
name= inputall.name
pwd= inputall.pwd
print print name ,pwd
就得到了页面提交的 name 和pwd

热点内容
单片机android 发布:2024-09-20 09:07:24 浏览:764
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:663
如何更换服务器网站 发布:2024-09-20 08:42:34 浏览:310
子弹算法 发布:2024-09-20 08:41:55 浏览:288
手机版网易我的世界服务器推荐 发布:2024-09-20 08:41:52 浏览:816
安卓x7怎么边打游戏边看视频 发布:2024-09-20 08:41:52 浏览:161
sql数据库安全 发布:2024-09-20 08:31:32 浏览:93
苹果连接id服务器出错是怎么回事 发布:2024-09-20 08:01:07 浏览:506
编程键是什么 发布:2024-09-20 07:52:47 浏览:657
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:480