我已經做了一個功能,以刮取網站。該功能會刮掉網站並獲取網站中的網址。如何刪除特定域名或字符串的url鏈接
print links #scrape() http://www.web1.to/something http://www.web2.gov.uk/something http://www.web3.com/something http://www.web4.com/something http://www.web5.com/something http://www.web6.com/something
擷取它也不必要檢索網站的鏈接或串.RDF我想刪除。
def scrape()
.
.
links = re.findall('href="(http.*?)"', sourceCode)
for link in set(links):
if 'web1.to' in link:
pass
elif 'web2.gov.' in link:
pass
elif '.rdf' in link:
pass
else:
return link
#print link; #it seems to work(*)
#this section which call scrape function and prints the links
for web in scrape():
print web
time.sleep(1)
我創造了這個功能,這似乎如果我使用的打印功能颳去裏面工作(見註釋行#PRINT鏈接)。但是,當我把它叫做外它只返回一個URL
http://www.web6.com/something
我然後取出for循環
if 'web1.to' in link:
pass
elif 'web2.gov.' in link:
pass
elif 'web3.com' in link:
pass
else:
return link
使用此功能體改從外部打印
。我在這裏給出的條件不起作用,它打印所有的網站。
我知道我在編碼時犯了一些邏輯錯誤,但我沒有看到它。你能幫我嗎
無法正常工作。它的劑量相同,只打印一個鏈接。 – Eka
@Eka你確定你的縮進是正確的嗎?確保你在for循環結束後返回*,而不是在追加有效鏈接後返回。我現在將添加一個示例 –
你知道了我做了一個縮進錯誤。我把回報放在其他聲明之下。謝謝 :) – Eka