我正在从.csv文件中读取网址,并且我正在尝试解析它们。当我在函数urlparse(...)
中明确显示链接时,为什么我只在方案和 netloc 中获得正确的值,请参阅变量o2
,而不是在我在newsource
中提供了urlparse
?
for line in file:
source = str(line.split(",")[2])
print("ORIGINAL URL: \n" + source)
newsource = source.replace('"',"")
print("REMOVING QUOTES: \n" + newsource)
newsource.strip
print("STRIPPING SPACES: \n" + newsource + "\n")
o = urlparse(newsource)
print("RESULT PARSING: " + str(o) + "\n")
o2 = urlparse("http://nl.aldi.be/aldi_vlees_609.html")
print("RESULT MANUAL PARSING: " + str(o2) + "\n")
答案 0 :(得分:1)
我可以从失败的解析中看到你有一个前导空格字符,这会导致你遇到同样的问题:
>>> urlparse.urlparse(' http://nl.aldi.be/aldi_vlees_609.html')
ParseResult(scheme='', netloc='', path=' http://nl.aldi.be/aldi_vlees_609.html', params='', query='', fragment='')
此行没有任何作用:
newsource.strip
你可能想要:
newsource = newsource.strip()