【悬赏50鱼币】xpath解析后中文变成unicode十进制编码的问题【回复帮顶有惊喜】
本帖最后由 米多花 于 2019-8-10 17:02 编辑代码如下:
import requests
from lxml import etree
url='http://www.jk-lawyer.com/detail_6027.html'
resp=requests.get(url)
html=etree.HTML(resp.content.decode())
li=html.xpath("//div[@class='detail']/div[@class='d_p']/*")
print(''.join(etree.tostring(tmp).decode() for tmp in li))
结果如下:
<p>
<span style="font-size:16px;line-height:24px;"> 应泰州市食品药品监督管理局</span><span style="font-size:16px;line-height:24px;">领导邀请,本所主任王金宝律师于2016年10月19日下午为泰州全市食品药品监管系统食品安全监管人员培训班全体学员以《新食品安全法实用导读》讲授了已于去年10月1日实施的新《食品安全法》,重点讲授了新法背景下食品安全监管的四十要素、新法规定的法律责任概览以及食品安全行政处罚原则等,尤其是针对基层监管执法人员应当掌握和注意的事项。王主任的讲课重点突出,务实致用,且生动活泼,现场互动频繁,受到了泰州市局领导以及全体与会人员的高度评价。</span>
</p>
<p style="text-align:center;">
<span style="font-size:16px;line-height:24px;"><img src="/ddadmin/Editor/attached/image/20170107/20170107231540_72363.jpg" alt="" /><br />
</span>
</p><div></div>
转存为HTML文档后可以正常显示,所有的可显示字符全部转变成了类似&#xxxxx的unicode十进制编码,试了很多办法都没能把它们变回来,有谁碰到过同类的问题,帮助解答一下。
请看清楚题目,我需要把&#xxxxx转换为汉字,我会另外写函数处理,但是我想找到xpath或者requests库里自带的方法,肯定有,我不知道而已。
回复帮顶50%机率获得1鱼币,最佳答案奉送50鱼币
Unicode试试这个 031444068013237.png 031443137705605.png 文本内容为:
应泰州市食品药品监督管理局领导邀请,本所主任王金宝律师于2016年10月19日下午为泰州全市食品药品监管系统食品安全监管人员培训班全体学员以《新食品安全法实用导读》讲授了已于去年10月1日实施的新《食品安全法》,重点讲授了新法背景下食品安全监管的四十要素、新法规定的法律责任概览以及食品安全行政处罚原则等,尤其是针对基层监管执法人员应当掌握和注意的事项。王主任的讲课重点突出,务实致用,且生动活泼,现场互动频繁,受到了泰州市局领导以及全体与会人员的高度评价。
办法很简单,把楼主贴出的代码照原样复制粘贴到 Windows 记事本,然后保存磁盘文件,把扩展名改为 .html 即可,最后在 google chrome 浏览器中打开这个文件,就得到了上面的结果。 jackz007 发表于 2019-8-10 12:38
文本内容为:
办法很简单,把楼主贴出的代码照原样复制粘贴到 Windows 记事本,然后保存磁 ...
我知道这个方法,但是插入到数据库中时,增加的存储太多了,我需要把&#xxxxx变成汉字 falseh 发表于 2019-8-10 12:27
Unicode试试这个
有没有具体代码 虽然不知道,但帮忙顶一下{:10_256:} 顶一个 本帖最后由 jackz007 于 2019-8-10 13:23 编辑
米多花 发表于 2019-8-10 12:47
我知道这个方法,但是插入到数据库中时,增加的存储太多了,我需要把&#xxxxx变成汉字
s = '''<p>
<span style="font-size:16px;line-height:24px;"> 应泰州市食品药品监督管理局</span><span style="font-size:16px;line-height:24px;">领导邀请,本所主任王金宝律师于2016年10月19日下午为泰州全市食品药品监管系统食品安全监管人员培训班全体学员以《新食品安全法实用导读》讲授了已于去年10月1日实施的新《食品安全法》,重点讲授了新法背景下食品安全监管的四十要素、新法规定的法律责任概览以及食品安全行政处罚原则等,尤其是针对基层监管执法人员应当掌握和注意的事项。王主任的讲课重点突出,务实致用,且生动活泼,现场互动频繁,受到了泰州市局领导以及全体与会人员的高度评价。</span>
</p>
<p style="text-align:center;">
<span style="font-size:16px;line-height:24px;"><img src="/ddadmin/Editor/attached/image/20170107/20170107231540_72363.jpg" alt="" /><br />
</span>
</p><div></div>'''
s = '' . join(s . split('\n'))
txt = ''
for x in s . split(';'):
x = x . strip()
if x[: 2] == '&#':
x = int(x)
if x < 128 or x > 255:
txt += unichr(x)
print(txt)
以下是是在 Python 2.7.14 环境下的运行实况:
C:\Bin>python xxxx.py
应泰州市食品药品监督管理局导邀请,本所主任王金宝律师于下午为泰州全市食品药品监管
系统食品安全监管人员培训班全体学员以《新食品安全法实用导读》讲授了已于去年实施的
新《食品安全法》,重点讲授了新法背景下食品安全监管的四十要素、新法规定的法律责任
概览以及食品安全行政处罚原则等,尤其是针对基层监管执法人员应当掌握和注意的事项。
王主任的讲课重点突出,务实致用,且生动活泼,现场互动频繁,受到了泰州市局领导以及
全体与会人员的高度评价。
C:\Bin> 帮顶 jackz007 发表于 2019-8-10 12:57
以下是是在 Python 2.7.14 环境下的运行实况:
谢谢你的热心,这个方法我已经有的,我定义了一个函数。我觉得这个方法效率较低,不想采用,我觉得requests和xpath库里肯定有更高效的,甚至只是一个附带的参数而已。我的函数如下:
import re
def to_unicode(html):
for chat in re.findall(r'&#(\d{2}(?:\d{3})?);',html):
html=html.replace('&#%s;'%chat,chr(int(chat)))
return html
if __name__ == "__main__":
s=s = '''<p>
<span style="font-size:16px;line-height:24px;"> 应泰州市食品药品监督管理局</span><span style="font-size:16px;line-height:24px;">领导邀请,本所主任王金宝律师于2016年10月19日下午为泰州全市食品药品监管系统食品安全监管人员培训班全体学员以《新食品安全法实用导读》讲授了已于去年10月1日实施的新《食品安全法》,重点讲授了新法背景下食品安全监管的四十要素、新法规定的法律责任概览以及食品安全行政处罚原则等,尤其是针对基层监管执法人员应当掌握和注意的事项。王主任的讲课重点突出,务实致用,且生动活泼,现场互动频繁,受到了泰州市局领导以及全体与会人员的高度评价。</span>
</p>
<p style="text-align:center;">
<span style="font-size:16px;line-height:24px;"><img src="/ddadmin/Editor/attached/image/20170107/20170107231540_72363.jpg" alt="" /><br />
</span>
</p><div></div>'''
print(to_unicode(s)) 回复错了
应是建一个新建文本文档.txt
把你的结果复制进去并保存
改为新建文本文档.html就可以显示你想要的了 为表歉意给你们发个游戏吧:
#导入模块
import pygame
from pygame.locals import *
import sys,random,time,math
class GameWindow(object):
'''创建游戏窗口类'''
def __init__(self,*args,**kw):
self.window_length = 600
self.window_wide = 500
#绘制游戏窗口,设置窗口尺寸
self.game_window = pygame.display.set_mode((self.window_length,self.window_wide))
#设置游戏窗口标题
pygame.display.set_caption("CatchBallGame")
#定义游戏窗口背景颜色参数
self.window_color = (135,206,250)
def backgroud(self):
#绘制游戏窗口背景颜色
self.game_window.fill(self.window_color)
class Ball(object):
'''创建球类'''
def __init__(self,*args,**kw):
#设置球的半径、颜色、移动速度参数
self.ball_color = (255,215,0)
self.move_x = 1
self.move_y = 1
self.radius = 10
def ballready(self):
#设置球的初始位置、
self.ball_x = self.mouse_x
self.ball_y = self.window_wide-self.rect_wide-self.radius
#绘制球,设置反弹触发条件
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)
def ballmove(self):
#绘制球,设置反弹触发条件
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)
self.ball_x += self.move_x
self.ball_y -= self.move_y
#调用碰撞检测函数
self.ball_window()
self.ball_rect()
#每接5次球球速增加一倍
if self.distance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.move_x += self.move_x
self.move_y += self.move_y
self.point += self.point
#设置游戏失败条件
if self.ball_y > 520:
self.gameover = self.over_font.render("Game Over",False,(0,0,0))
self.game_window.blit(self.gameover,(100,130))
self.over_sign = 1
class Rect(object):
'''创建球拍类'''
def __init__(self,*args,**kw):
#设置球拍颜色参数
self.rect_color = (255,0,0)
self.rect_length = 100
self.rect_wide = 10
def rectmove(self):
#获取鼠标位置参数
self.mouse_x,self.mouse_y = pygame.mouse.get_pos()
#绘制球拍,限定横向边界
if self.mouse_x >= self.window_length-self.rect_length//2:
self.mouse_x = self.window_length-self.rect_length//2
if self.mouse_x <= self.rect_length//2:
self.mouse_x = self.rect_length//2
pygame.draw.rect(self.game_window,self.rect_color,((self.mouse_x-self.rect_length//2),(self.window_wide-self.rect_wide),self.rect_length,self.rect_wide))
class Brick(object):
def __init__(self,*args,**kw):
#设置砖块颜色参数
self.brick_color = (139,126,102)
self.brick_list = [,,,,]
self.brick_length = 80
self.brick_wide = 20
def brickarrange(self):
for i in range(5):
for j in range(6):
self.brick_x = j*(self.brick_length+24)
self.brick_y = i*(self.brick_wide+20)+40
if self.brick_list == 1:
#绘制砖块
pygame.draw.rect(self.game_window,self.brick_color,(self.brick_x,self.brick_y,self.brick_length,self.brick_wide))
#调用碰撞检测函数
self.ball_brick()
if self.distanceb < self.radius:
self.brick_list = 0
self.score += self.point
#设置游戏胜利条件
if self.brick_list == [,,,,]:
self.win = self.win_font.render("You Win",False,(0,0,0))
self.game_window.blit(self.win,(100,130))
self.win_sign = 1
class Score(object):
'''创建分数类'''
def __init__(self,*args,**kw):
#设置初始分数
self.score = 0
#设置分数字体
self.score_font = pygame.font.SysFont('arial',20)
#设置初始加分点数
self.point = 1
#设置初始接球次数
self.frequency = 0
def countscore(self):
#绘制玩家分数
my_score = self.score_font.render(str(self.score),False,(255,255,255))
self.game_window.blit(my_score,(555,15))
class GameOver(object):
'''创建游戏结束类'''
def __init__(self,*args,**kw):
#设置Game Over字体
self.over_font = pygame.font.SysFont('arial',80)
#定义GameOver标识
self.over_sign = 0
class Win(object):
'''创建游戏胜利类'''
def __init__(self,*args,**kw):
#设置You Win字体
self.win_font = pygame.font.SysFont('arial',80)
#定义Win标识
self.win_sign = 0
class Collision(object):
'''碰撞检测类'''
#球与窗口边框的碰撞检测
def ball_window(self):
if self.ball_x <= self.radius or self.ball_x >= (self.window_length-self.radius):
self.move_x = -self.move_x
if self.ball_y <= self.radius:
self.move_y = -self.move_y
#球与球拍的碰撞检测
def ball_rect(self):
#定义碰撞标识
self.collision_sign_x = 0
self.collision_sign_y = 0
if self.ball_x < (self.mouse_x-self.rect_length//2):
self.closestpoint_x = self.mouse_x-self.rect_length//2
self.collision_sign_x = 1
elif self.ball_x > (self.mouse_x+self.rect_length//2):
self.closestpoint_x = self.mouse_x+self.rect_length//2
self.collision_sign_x = 2
else:
self.closestpoint_x = self.ball_x
self.collision_sign_x = 3
if self.ball_y < (self.window_wide-self.rect_wide):
self.closestpoint_y = (self.window_wide-self.rect_wide)
self.collision_sign_y = 1
elif self.ball_y > self.window_wide:
self.closestpoint_y = self.window_wide
self.collision_sign_y = 2
else:
self.closestpoint_y = self.ball_y
self.collision_sign_y = 3
#定义球拍到圆心最近点与圆心的距离
self.distance = math.sqrt(math.pow(self.closestpoint_x-self.ball_x,2)+math.pow(self.closestpoint_y-self.ball_y,2))
#球在球拍上左、上中、上右3种情况的碰撞检测
if self.distance < self.radius and self.collision_sign_y == 1 and (self.collision_sign_x == 1 or self.collision_sign_x == 2):
if self.collision_sign_x == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_x == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_x == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_x == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:
self.move_y = - self.move_y
#球在球拍左、右两侧中间的碰撞检测
if self.distance < self.radius and self.collision_sign_y == 3:
self.move_x = - self.move_x
#球与砖块的碰撞检测
def ball_brick(self):
#定义碰撞标识
self.collision_sign_bx = 0
self.collision_sign_by = 0
if self.ball_x < self.brick_x:
self.closestpoint_bx = self.brick_x
self.collision_sign_bx = 1
elif self.ball_x > self.brick_x+self.brick_length:
self.closestpoint_bx = self.brick_x+self.brick_length
self.collision_sign_bx = 2
else:
self.closestpoint_bx = self.ball_x
self.collision_sign_bx = 3
if self.ball_y < self.brick_y:
self.closestpoint_by = self.brick_y
self.collision_sign_by = 1
elif self.ball_y > self.brick_y+self.brick_wide:
self.closestpoint_by = self.brick_y+self.brick_wide
self.collision_sign_by = 2
else:
self.closestpoint_by = self.ball_y
self.collision_sign_by = 3
#定义砖块到圆心最近点与圆心的距离
self.distanceb = math.sqrt(math.pow(self.closestpoint_bx-self.ball_x,2)+math.pow(self.closestpoint_by-self.ball_y,2))
#球在砖块上左、上中、上右3种情况的碰撞检测
if self.distanceb < self.radius and self.collision_sign_by == 1 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
if self.collision_sign_bx == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:
self.move_y = - self.move_y
#球在砖块下左、下中、下右3种情况的碰撞检测
if self.distanceb < self.radius and self.collision_sign_by == 2 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
if self.collision_sign_bx == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:
self.move_y = - self.move_y
#球在砖块左、右两侧中间的碰撞检测
if self.distanceb < self.radius and self.collision_sign_by == 3:
self.move_x = - self.move_x
class Main(GameWindow,Rect,Ball,Brick,Collision,Score,Win,GameOver):
'''创建主程序类'''
def __init__(self,*args,**kw):
super(Main,self).__init__(*args,**kw)
super(GameWindow,self).__init__(*args,**kw)
super(Rect,self).__init__(*args,**kw)
super(Ball,self).__init__(*args,**kw)
super(Brick,self).__init__(*args,**kw)
super(Collision,self).__init__(*args,**kw)
super(Score,self).__init__(*args,**kw)
super(Win,self).__init__(*args,**kw)
#定义游戏开始标识
start_sign = 0
while True:
self.backgroud()
self.rectmove()
self.countscore()
if self.over_sign == 1 or self.win_sign == 1:
break
#获取游戏窗口状态
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
if pressed_array:
start_sign = 1
if start_sign == 0:
self.ballready()
else:
self.ballmove()
self.brickarrange()
#更新游戏窗口
pygame.display.update()
#控制游戏窗口刷新频率
time.sleep(0.010)
if __name__ == '__main__':
pygame.init()
pygame.font.init()
catchball = Main()
不知你们喜不喜欢 我也不懂
来学习一下 from html import unescape
import requests
from lxml import etree
url='http://www.jk-lawyer.com/detail_6027.html'
resp=requests.get(url)
html=etree.HTML(resp.content.decode())
li=html.xpath("//div[@class='detail']/div[@class='d_p']/*")
print(unescape(''.join(etree.tostring(tmp).decode() for tmp in li))) kaohsing 发表于 2019-8-10 14:48
结帖,18楼为最佳答案,加好友送鱼币。不过可惜还是使用了第三方库,如果有只用xpath库解决的,继续送50鱼币 帮顶 帮顶一下
页:
[1]
2