使用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