简体   繁体   中英

Trouble with requests' get in Python

I am trying to automatically download files from a website using a list of URLs that I already have. The relevant part of my code looks like this:

for url in urls:
    if len(url) != 0:
        print url

Running this prints a list of urls as strings - as expected. However, when I add one new line as below:

for url in urls:
    if len(url) != 0:
        print url
        r = requests.get(url)

an error appears saying "Invalid URL u'Document Detail': No schema supplied." Before this breaks, it is supposed to print a url. Previously, this printed the url as expected. However, now it prints "Document Detail" instead of a URL. I'm not quite sure why this happens and how to resolve it.

Any help would be appreciated!

EDIT

urls = []
with open('filename.csv', 'rb') as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        urls.append(row[34])

With reference to my comment, "Document Details" is the header of your csv. Skip it. Here's one way to do it.

urls = []
with open('filename.csv', 'rb') as f:
    read = f.readlines()
    urls = [row.split(",")[34] for row in read[1:]]

您的csv文件的布局可能已更改,URL不再位于第33列,即(34-1,因为rows是从零开始的)。

The you should convert url to string explicitly:

for url in urls:
    if len(url) != 0:
        print str(url)
        r = requests.get(str(url))

And maybe you can give us some piece of your .csv file please.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM