1

I have some files that contain one URL per line, like

....

I try to code for:

import wget

with open ("5074_url.txt", encoding='utf-8', mode = 'r') as f:
    for line in list(f): # OR f.readlines()
        filename = wget.download(line)
        print (filename)

but raise Error msg:

Traceback (most recent call last):
  File ".\Geturl2.py\", line 33, in <module>
    filename = wget.download(line)
  File "C:\Program Files (x86)\Python\lib\site-packages\wget.py", line 506, in download
    (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
  File "C:\Program Files (x86)\Python\lib\tempfile.py", line 342, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "C:\Program Files (x86)\Python\lib\tempfile.py", line 260, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
OSError: [Errno 22] Invalid argument: '.\\page-1.jpg\ngjf6wrvy.tmp'

\ngjf6wrvy.tmp What's this ? There's no this in file, I'm sure that.

I'm using Python 3.6.5 on windows 10

I know it could use urllib.request.urlretrieve(url, filename) , but it has filename option. I don't wanna change filename. So How don't change filename ?

[Solved]

import wget

with open ("5074_url.txt", encoding='utf-8', mode = 'r') as f:
    for url in f.readlines():
        filename = wget.download(url.strip())
        print (filename)

Thank you so much for help !!!

Kanglando
  • 33
  • 8

1 Answers1

1

I don't know what you're trying to achieve exactly, but here's an example with requests:

#!/usr/bin/env python
import os

import requests


with open('test.txt', 'r') as f:
    for url in f.readlines():
        r = requests.get(url.strip())
        print(r)

This will "download" each URL contained in test.txt and store in memory. The variable r contains the Response object.

  • No any file to be downloaded. – Kanglando Apr 10 '18 at 11:17
  • It will download it an keep it in "memory" until you store it. So you need another `open()` of a new file and store the content in there. Or you go with the link proposed by @candah: https://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python – Dominique Barton Apr 10 '18 at 11:21
  • I try use "r = urllib.request.urlopen(url.strip())" , and "urlretrieve" . it does not works. – Kanglando Apr 10 '18 at 11:23
  • urllib.request.urlretrieve have contains filename option. I don't wanna change filename. So How ? – Kanglando Apr 10 '18 at 11:30
  • Thank you for your code about "url.strip()". it works fine when use wget . – Kanglando Apr 10 '18 at 11:33