0

I want to request a list of urls which have the following formats for example:

http://en.wikipedia.org/wiki/Monty_Python/001/

http://en.wikipedia.org/wiki/Monty_Python/002/

....

These codes 001,... I saved in a csv file.

import csv
import requests

inputfile = open('trycode.csv','r')
for row in inputfile:
    url = 'http://en.wikipedia.org/wiki/Monty_Python/'
    source = requests.get(url + row/)

And obviously it doesn't work... How can I correctly call these urls with the last part coming from the csv file?

Many thanks!

5
  • Apart from the extra '/' at the end, which is a syntax error, why does this "obviously" not work? Commented Jul 30, 2015 at 15:39
  • 1
    Your code throw exceptions or it works incorrect? Commented Jul 30, 2015 at 15:39
  • 2
    Maybe you mean url + row + '/' ? Or '%s%s/' % (url, row)? Commented Jul 30, 2015 at 15:40
  • @DanielRoseman The problem is the url in the end contains a / , so I don't know how to put it. without the / , the url add is not correct. Commented Jul 30, 2015 at 16:02
  • @larsks yes, you are right, I tried to put instead now url + row +'/', but when I print the url link, en.wikipedia.org/wiki/Monty_Python/00%A2, not en.wikipedia.org/wiki/Monty_Python/002, Commented Jul 30, 2015 at 16:07

1 Answer 1

1

Are you actually using a Comma Separated Value format for your file? Seems if just a short string, I'd use a txt file, but that's just me.

It's always a good idea to build your string URL before trying to call it... this way you can inspect it first... also important to close your file:

import csv
import requests

inputfile = open('trycode.csv','r')
for row in inputfile:
    url = 'http://en.wikipedia.org/wiki/Monty_Python/' + row
    source = requests.get(url)
inputfile .close()

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! But when I tried your code, for example using a row entry "01020" I always get en.wikipedia.org/wiki/Monty_Python/01020%0A, ideally I want en.wikipedia.org/wiki/Monty_Python/010200, maybe something wrong with my file format?
Looks like &0A is a URL-encoded newline character... check your source file to ensure that you have clean data, or Replace the %0A before using the value from each line.
Thanks! I will take a look at the file to see if its a clean txt file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.