【发布时间】:2009-08-12 21:15:29
【问题描述】:
我是 Python 新手(我也没有接受过任何编程培训),所以在我提问时请记住这一点。
我正在尝试搜索检索到的网页并使用指定模式查找所有链接。我已在其他脚本中成功完成此操作,但我收到一条错误消息,提示
raise error, v # invalid expressionsre_constants.error: 多次重复
我不得不承认我不知道为什么,但我还是 Python 和正则表达式的新手。但是,即使我不使用模式并使用特定链接(只是为了测试匹配),我也不相信我会返回任何匹配项(当我打印 match.group(0) 时不会将任何内容发送到窗口。链接我测试的在下面被注释掉了。
有什么想法吗?通过示例学习通常对我来说更容易,但非常感谢您提供的任何建议!
布洛克
import urllib2
from BeautifulSoup import BeautifulSoup
import re
url = "http://forums.epicgames.com/archive/index.php?f-356-p-164.html"
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
pattern = r'<a href="http://forums.epicgames.com/archive/index.php?t-([0-9]+).html">(.?+)</a> <i>((.?+) replies)'
#pattern = r'href="http://forums.epicgames.com/archive/index.php?t-622233.html">Gears of War 2: Horde Gameplay</a> <i>(20 replies)'
for match in re.finditer(pattern, page, re.S):
print match(0)
【问题讨论】: