使用BeautifulSoup刮页;试图筛选出在最终环节 “... HTML#评论”语法错误 - Python re.search(字符类,插入符号)
代码如下:
import urllib.request
import re
from bs4 import BeautifulSoup
base_url = "http://voices.washingtonpost.com/thefix/morning-fix/"
soup = BeautifulSoup(urllib.request.urlopen(base_url)).findAll('a')
links_to_follow = []
for i in soup:
if i.has_key('href') and \
re.search(base_url, i['href']) and \
len(i['href']) > len(base_url) and \
re.search(r'[^(comments)]', i['href']):
print(i['href'])
的Python 3.2,Windows 7的64位。
以上脚本保存在“#comments”
我试过re.search([^comments], i['href'])
,re.search([^(comments)], i['href'])
和re.search([^'comments'], i['href'])
结尾的链接 - 所有扔语法错误。
对Python来说很陌生,所以对于平庸的道歉。 (a)我对'r'前缀的正确理解不够详细,或者 (b)响应[^(foo)] re.search返回的不是该集合排除'foo'的行,但是只包含多于'foo'的行。例如,我保留我的...#注释链接,因为... texttexttext.html#注释先于它或 (c)Python将“#”解释为结束re.search应匹配的行的注释。
我觉得我错了(b)。
对不起,知道这很简单。谢谢,
扎克
你应该包括你得到的错误/回溯的确切文本。 – Amber 2012-03-24 18:49:05