在Python爬蟲中,使用正則表達式處理重復內容時,可以使用re.findall()
函數。這個函數會返回所有匹配到的子串列表。如果需要處理重復的內容,可以在正則表達式中使用非貪婪匹配、正向預查或反向預查等技巧。
以下是一些示例:
import re
text = "這是一個包含多個<span>重復標簽</span>的文本。"
pattern = r"<span.*?>.*?</span>"
matches = re.findall(pattern, text)
print(matches)
import re
text = "這是一個包含多個<span class='example'>重復標簽</span>的文本。"
pattern = r"(<span class='example'>.*?)</span>"
matches = re.findall(pattern, text)
print(matches)
import re
text = "這是一個包含多個<span>重復標簽</span>的文本。"
pattern = r".*?<span>(.*?)</span>"
matches = re.findall(pattern, text)
print(matches)
在這些示例中,我們使用了不同的正則表達式技巧來處理重復的<span>
標簽。你可以根據自己的需求選擇合適的方法。