【问题标题】:Cant get PHP to accept the POUND sign from a beautiful soup Python script无法让 PHP 接受来自漂亮汤 Python 脚本的 POUND 符号
【发布时间】:2012-07-11 22:48:53
【问题描述】:

所以我有一个从活动网页中提取信息的脚本。网址是这样的:http://everguide.com.au/melbourne/event/2012-jul-14/colour/

这个 php 脚本正在调用一个 python 脚本(它是 for 循环的一部分):

${"tmp" . $i} = utf8_encode (exec("python myscrape.py ${"eu" . $i}"));

它传递一个 URL。 python脚本是这样的:

# -*- coding: utf-8 -*-
import sys
URL = sys.argv[1]
#$URL = 'http://everguide.com.au/melbourne/event/2012-jul-14/colour/'

import urllib2
req = urllib2.Request(URL)
response = urllib2.urlopen(req)
html = response.read()

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(html.decode('utf-8'))
soup.prettify()

import re


for node in soup.findAll(itemprop="name"):
    n = ''.join(node.findAll(text=True)) 
for node in soup.findAll(itemprop="url"):
    v = ''.join(node.findAll(text=True))

for node in soup.findAll("div", { "class" : "time" }):
    d = ''.join(node.findAll(text=True))

for node in soup.findAll("a", { "id" : "ctl00_holderBody_ctl00_lnkCat" }):
    c = ''.join(node.findAll(text=True)) 

vu = v
vu.encode('utf-8', 'xmlcharrefreplace')
re.escape(vu)

print n,"|", d,"|", vu,"|", c

效果非常好,但只能在 VU 之前返回到 or 管道 - 它不能超过那个!

所有文件、HTML 和 php 都设置了 UTF-8 编码。

当 V 变量中有特殊字符时,它会中断并停止。如果没有特殊字符,则可以完美运行。

预期输出是:

Colour | 14 July @ 7:30PM | 1000 £ Bend | Clubs & Parties

在服务器上运行脚本(使用相同的 python 命令)但通过 PHP 时可以看到此输出 - 我无法恢复 Venue 字符串!

请帮忙

瑞克

【问题讨论】:

  • 您是否尝试将 htmlspecialchars 添加到变量中?
  • 感谢丹尼斯 - 除了玛丽亚的回答解决了这个问题。非常感谢您的帮助!

标签: php python utf-8 beautifulsoup utf


【解决方案1】:

vu.encode 返回编码字符串...因为您没有分配编码结果,这只是被扔掉了。你试过了吗

vu = vu.encode('utf-8', 'xmlcharrefreplace')

您还需要跳过转义,因为它会弄乱编码的 unicode。

【讨论】:

  • 谢谢玛丽亚。有了 Dainis 关于确保 htmlspecialchars() 在数据被接收回 PHP 后立即在整个商店中设置到位的评论 - 你是对的,它工作得很好!如果 Beautifulsoup 文档提供了真实的使用示例,而不是简单的理论,我怀疑这个问题会发生!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 2018-12-17
  • 2013-03-21
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
相关资源
最近更新 更多