URL 仅解析工作显式

URL parsing only working explicit

我正在从 .csv 文件中读取 URL 并尝试解析它们。当我将 link 显式放入函数 urlparse(...) 时,为什么我只能在 schemenetloc 中获得正确的值,看到变量 o2 而不是当我在 urlparse?

中给出 newsource
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")

输出:

我可以从失败的解析中看出你有一个前导 space 字符,这会导致你遇到同样的问题:

>>> 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()