我正在寫一個web刮板來從網站上刮取鏈接。它工作正常,但輸出鏈接不乾淨。它輸出損壞的html鏈接,也檢索相同的html鏈接。這是代碼在python中清除廢棄的url
links = re.findall('<a class=.*?href="?\'?([^"\'>]*)', sourceCode)
for link in links:
print link
這是輸出的樣子
/preferences?hl=en&someting
/preferences?hl=en&someting
/history/something
/history/something
/support?pr=something
/support?pr=something
http://www.web1.com/parameters
http://www.web1.com/parameters
http://www.web2.com/parameters
http://www.web2.com/parameters
我試圖清理未HTML中使用這個正則表達式
link = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[[email protected]&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', link)
print link
它清潔網址,但增加了方形鏈支架。如何清理這個沒有方括號的地方?我應該如何阻止打印相同的URL兩次或多次
/preferences?hl=en&someting -> []
http://www.web1.com/parameters -> [http://www.web1.com/parameters]
不是一個解決方案,但一個提示:如果你使用'Python'無論如何,你很可能嘗試[Scrapy(http://scrapy.org),這使所有這些要求開箱即用(防止重複,建立一個合適的URL等)。 – Jan