First, you are skipping the first line. You might want to include it, like so:
with open("imagelink.txt") as url:
for url2 in url.readlines():
# do stuff
else :
with open("imagelink.txt") as url:
line = url.readline()
while line:
# do stuff
line = url.readline()
Then, you might need to check if your line is or has one or several url strings. You could use a regular expression
for that.
import re
# Example of regex pattern matching urls. You can find more defined ones, if you need.
url_pattern = r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
with open("imagelink.txt") as url:
line = url.readline()
while line:
for link in re.findall(url_pattern, line):
u = urllib.request.urlopen(link)
# do other stuff
line = url.readline()
The second inner for loop
insures you don't try to do anything with something that is not an url. If no url was found, the inner loop will break and pass to the next line.
Thanks to @acw1668 for the correction on url.read_line
vs url.readlines
.