我正在做一些需要我获取页面上所有 URL 的事情。它似乎适用于我测试过的大多数网站,例如 microsoft.com,但它只从 google.com 返回三个。以下是相关的源代码:
import urllib
import time
import re
fwcURL = "http://www.microsoft.com" #URL to read
mylines = urllib.urlopen(fwcURL).readlines()
print "Found URLs:"
time.sleep(1) #Pause execution for a bit
for item in mylines:
if "http://" in item.lower(): #For http
print item[item.index("http://"):].split("'")[0].split('"')[0] # Remove ' and " from the end, for example in href=
if "https://" in item.lower(): #For https
print item[item.index("https://"):].split("'")[0].split('"')[0] # Ditto
如果我的代码可以改进,或者如果有更好的方法可以做到这一点,请回复。提前致谢!