Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用BeautifulSoup解析不常见符号_Python_Regex_Beautifulsoup - Fatal编程技术网

Python 使用BeautifulSoup解析不常见符号

Python 使用BeautifulSoup解析不常见符号,python,regex,beautifulsoup,Python,Regex,Beautifulsoup,我有一个类似于的链接,其中有一个不同寻常的符号',它甚至不存在于标准英文键盘中。 它是Ctrl+k在此编辑器中生成的符号的镜像。 所以在我运行stackoverflow上的代码之后: soup = BeautifulSoup.BeautifulSoup("<a href=abc.asp?xyz=foobar&baz=lookatme´_beautiful.jpg>"); for a in soup.findAll('a'):

我有一个类似于
的链接,其中有一个不同寻常的符号
'
,它甚至不存在于标准英文键盘中。 它是
Ctrl+k
在此编辑器中生成的符号的镜像。 所以在我运行stackoverflow上的代码之后:

soup = BeautifulSoup.BeautifulSoup("<a href=abc.asp?xyz=foobar&baz=lookatme´_beautiful.jpg>");
for a in soup.findAll('a'):                                                                       
    print a['href']
import re
urls = re.findall(r'href=[\'"]?([^\'" >]+)', s)

如果无法修改beautifulsoup正则表达式,如何修改上述正则表达式以合并
\xb4
符号。(这里是有问题的字符串)

您可以在re模式中包含[\u0000-\uFFFF]作为子范围,或者只包含\xb4作为[\u00b4]升级到最新版本的BeautifulSoup并安装
html5lib
,这是一个非常宽松的解析器:

import requests
from bs4 import BeautifulSoup

html = requests.get('http://www.atlasdermatologico.com.br/listar.asp?acao=indice').text
soup = BeautifulSoup(html, 'html5lib')

for a in soup.find_all('a'):
    href = a.get('href')

    if '\\' in repr(href):
        print(repr(href))

它会正确地打印出URL中带有
\xb4
的链接。

您的regexp不关心下面的内容
href=
,只要它以空格结尾(或以引号结尾),因此它与\xb4匹配,就像任何其他字符一样:

>>> s = "<a href=abc.asp?xyz=foobar&baz=lookatme\xb4_beautiful.jpg>"
>>> print s.decode("latin-1")
<a href=abc.asp?xyz=foobar&baz=lookatme´_beautiful.jpg>
>>> urls = re.findall(r'href=[\'"]?([^\'" >]+)', s)
>>> urls
['abc.asp?xyz=foobar&baz=lookatme\xb4_beautiful.jpg']

>s=)

你能发一个链接到这个网页吗?--这是stackoverflow网站,这是我试图刮的网站——不要查看网页中的其他链接;仅限医疗专业人员使用。我无法将%B4s合并到正则表达式中,我在python字符串中看到\xb4的字符串表示形式已转义。\ub4应该是\xb4或\u00b4,对吗\u期望4个十六进制数字,就像在第一个例子中一样。谢谢它工作得很好,但是有一个问题如何说服我的浏览器考虑实际上是一个链接,即如何用%B4s替代。在Chrome中点击那个特定的链接可以正确地呈现网页,但是如果我真的把那个链接放到omnibox中,它就不起作用了。Python中是否有任何内置函数可以帮助我。