【问题标题】:Python Beautifulsoup not finding regular expressionPython Beautifulsoup 找不到正则表达式
【发布时间】:2014-11-09 09:23:55
【问题描述】:

这已经困扰我一段时间了,我无法使用正则表达式在 Beautifulsoup 中查找字符串,我不知道为什么。

这是我遇到问题的线路:

data = soup.find(text=re.compile('Överförda data (skickade/mottagna) 

如果需要,这里是整个代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from bs4 import BeautifulSoup

import re
import urllib2

# Fetch URL
url = 'http://192.168.1.254/cgi/b/bb/?be=0&l0=1&l1=-1'
request = urllib2.Request(url)
request.add_header('Accept-Encoding', 'utf-8')

# Response has UTF-8 charset header,
# and HTML body which is UTF-8 encoded
response = urllib2.urlopen(request)

soup = BeautifulSoup(response)

time = soup.find(text="Aktiv tid:").findNext('td').contents[0]
data = soup.find(text=re.compile('Överförda data (skickade/mottagna) [GB/GB]:')).findNext('td').contents[0] # complains about this line

f=open('/var/www/log.txt', 'a')
print(time + ";" + data,file=f)
f.close()

每当我运行它时,就会出现一个 AttributeError 类型的错误,说 'NoneType' 对象没有属性 'findNext'

因为我的字符串可以是:

  • Överförda 数据 (skikade/mottagna) GB/GB:
  • Överförda 数据(skikade/mottagna)[MB/MB]:

所以我需要使用正则表达式来查看它是否匹配其中任何一个。

提前谢谢你!

编辑:我现在更改了我的代码(参见下面的answer),但它仍然给我同样的错误:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from bs4 import BeautifulSoup

import re
import urllib2

# Fetch URL
url = 'http://192.168.1.254/cgi/b/bb/?be=0&l0=1&l1=-1'
request = urllib2.Request(url)
request.add_header('Accept-Encoding', 'utf-8')

# Response has UTF-8 charset header,
# and HTML body which is UTF-8 encoded
response = urllib2.urlopen(request)

soup = BeautifulSoup(response)

time = soup.find(text="Aktiv tid:").findNext('td').contents[0]
data = soup.find(text=re.compile(re.escape(u'Överförda data (skickade/mottagna) [GB/GB]:'))).findNext('td').contents[0]

f=open('/var/www/log.txt', 'a')
print(time + ";" + data,file=f)
f.close()

这里是HTML file的相关部分:

<table width='100%' class='datatable' cellspacing='0' cellpadding='0'>
  <tr>
    <td>
    </td>
    <td width='30px'>
    </td>
    <td width='220px'>
    </td>
    <td width='50px'>
    </td>
  </tr>
  <tr>
    <td height='7' colspan='4'>
      <img src='/images/spacer.gif' width='1' height='7' border='0' alt=''>
    </td>
  </tr>
  <tr>
    <td width='170'>
      Aktiv tid: <!--This is a string I will search for.-->
    </td>
    <td colspan='3'>
      1 dag, 17:03:46 <!--This is a piece of information I need to obtain.-->
    </td>
  </tr>
  <tr>
    <td height='7' colspan='4'>
      <img src='/images/spacer.gif' width='1' height='7' border='0' alt=''>
    </td>
  </tr>
  <tr>
    <td width='170'>
      Bandbredd (upp/ned) [kbps/kbps]:
    </td>
    <td colspan='3'>
      1.058 / 21.373
    </td>
  </tr>
  <tr>
    <td height='7' colspan='4'>
      <img src='/images/spacer.gif' width='1' height='7' border='0' alt=''>
    </td>
  </tr>
  <tr>
    <td width='170'>
      Överförda data (skickade/mottagna) [GB/GB]: <!--This is another string I will search for.-->
    </td>
    <td colspan='3'>
      1,67 / 42,95 <!--This is another piece of information I need to obtain.-->
    </td>
  </tr>
</table>

)

【问题讨论】:

    标签: python regex beautifulsoup attributeerror nonetype


    【解决方案1】:

    BeautifulSoup 对 unicode 字符串进行操作,但您传入的是字节字符串正则表达式。为您的表达式使用 Unicode 文字:

    re.compile(re.escape(u'Överförda data (skickade/mottagna) [GB/GB]:'))
    

    我还使用re.escape() 来避免元字符(括号和方括号)被解释为正则表达式信息。

    Öö 的 UTF-8 编码只会匹配精确的字节序列

    >>> 'Överförda'
    '\xc3\x96verf\xc3\xb6rda'
    >>> u'Överförda'
    u'\xd6verf\xf6rda'
    >>> print u'Överförda'
    Överförda
    >>> import re
    >>> re.search('Överförda', u'Överförda data (skickade/mottagna) [GB/GB]')
    >>> re.search(u'Överförda', u'Överförda data (skickade/mottagna) [GB/GB]')
    <_sre.SRE_Match object at 0x107d47ed0>
    

    这确实需要您在文件顶部做出正确的源代码编码声明,请参阅PEP 263

    【讨论】:

    • 谢谢,但我确实将编码声明为 utf-8,并使用了这样的 Unicode 文字:re.compile(u'Överförda data (skickade/mottagna) [GB/GB]:') 但它仍然给了我相同的结果。
    • @Linus:啊,你也需要在你的表达式中转义元字符。
    • 我仍然遇到相同错误消息的问题(请参阅我的编辑)。
    • @Linus:你能给我们提供任何样本输入吗?
    • 我现在已经添加了我的 HTML 文件和相关部分。
    【解决方案2】:

    方括号和圆括号在正则表达式中是特殊的。如果你想匹配那些文字字符(相对于定义捕获组、字符类等),你需要用反斜杠转义它们。

    【讨论】:

    • 我还需要转义正斜杠吗?
    • 不,正斜杠没问题。我在 Linux 上使用 Python 2.6.6,这对我有用:&gt;&gt;&gt; import re;pat=re.compile(u'Överförda');m=pat.search(u'Överförda data (skickade/mottagna) [GB/GB]');print m.group() Överförda
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2015-03-24
    相关资源
    最近更新 更多