0

I have a base url that I would like to append list values to so that I have a list of URLs to scrape. My list comes from a json file, and looks like:

[{u'url': [u'/location/subfile/file1.htm', u'/location/subfile/file2.htm', u'/location/subfile/file3.htm', u'/location/subfile/file4.htm']}]

My base URL is something like http://example.com/placeforfiles/

What I want, ultimately, is a collection of URLs that have that base URL plus the list values, like so:

http://example.com/placeforfiles/location/subfile/file1.htm
http://example.com/placeforfiles/location/subfile/file2.htm
http://example.com/placeforfiles/location/subfile/file3.htm
http://example.com/placeforfiles/location/subfile/file4.htm

There may be thousands of list values I need to append, so I know I need to loop through them and append them but I haven't found a solution that works. I'm currently trying:

import json

with open ('returned_items.json') as links:
    data = json.load(links)

base_url = 'http://example.com/placeforfiles/{}'

for i in data:
    url = 'http://example.com/placeforfiles/{}'.format(i)
    print url

Which is returning:

http://example.com/placeforfiles/({u'url': [u'/location/subfile/file1.htm', u'/location/subfile/file2.htm', u'/location/subfile/file3.htm', u'/location/subfile/file4.htm']},)
1
  • Sorry, just realized that was in there by accident I edited the question to remove it. Commented Jun 15, 2017 at 17:55

2 Answers 2

1

this is because the dict is the first element in the array. The loop should be for i in data[0]["url"]

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

Comments

1
#replcace data with below line
data = json.loads(links)

#replace your last loop with below
if data and 'url' in data[0]:
 for i in data[0]['url']:
  url = 'http://example.com/placeforfiles{}'.format(i)
  print(url)

Comments

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.