【问题标题】:Python 2.7 with BeautifulSoup Error: Cannot process flags argument with a compiled pattern带有 BeautifulSoup 错误的 Python 2.7:无法使用已编译模式处理标志参数
【发布时间】:2013-11-10 07:22:44
【问题描述】:

我正在尝试使用 BeautifulSoup 在网站中进行一些网络爬取。但是当我尝试获取 div 类 AddressInfo 中的内容时出现错误,这是我要抓取的网站的一部分:

<h4>Altônia</h4>
<div class="addressInfo">
Rua Getulio Vargas, 1201<br>
Centro - Iporã - PR<br>
87550-000<br>
<br>
(44) 3659-2721<br>
<a href="mailto:altoniacentro.pr@escolas.com.br">altoniacentro.pr@escolas.com.br</a><br>
</div>

这是我的代码:

from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import re

# Copy all of the content from the provided web page
webpage = urlopen('site url....').read()


# Grab everything that lies between the h4 tags using a REGEX
patFinderTitle = re.compile('<h4>(.*)</h4>')

# Grab everything that lies between the class addressInfo tags using a REGEX
patFinderAddress = re.compile('<div class="addressInfo">(.*)</div>') **<- get error here**

这是我得到的错误:

raise ValueError('Cannot process flags argument with a compiled pattern') ValueError: Cannot process flags argument with a compiled

模式

我该如何解决这个问题?

【问题讨论】:

  • 既然可以使用 BeautifulSoup 进行解析,为什么还要使用正则表达式?此外,请粘贴您的所有代码。您粘贴的代码看起来不完整,并且不会在此表单中产生错误。

标签: python regex python-2.7 beautifulsoup


【解决方案1】:

最好使用 xpath,它更容易: 试试这个:

 from lxml import html
    import requests

    url = 'http://.....'

    page = requests.get(url)
    tree = html.fromstring(page.text)

    a = tree.xpath('//h4/text()')
    b = tree.xpath('//div[@class="addressInfo"]/text()')
    c = tree.xpath('//a//text()')

    print  a, b, c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多