I’m learning Python and have written a little scraper in scrapy. For the moment I’m working with a hardcoded array list of urls, like so:
"http city1 /%(searchterm)s"dict(searchterm=searchterm),
"http city2 /%(searchterm)s"dict(searchterm=searchterm),
"http city3 /%(searchterm)s"dict(searchterm=searchterm),
I thought it is possible to reduce my code with parsing an array of cities (cities) into this url_array by calling the index of those cities from cities array where those cities are declared with an loop declaration inside the target array.
Is this technically possible? What should be my next step? What shall I Google?
cities = ["hamburg", "berlin", "muenchen"]
citiesCount = len(cities)
url_array = [
http:"%(cities[1])s"
while citiesCount in range(1, len(cities):
http:"%(cities[0+1]s"
]
Your example URLs dont make a lot of sense, and the question itself is (still, I edited away the worst of it) hard to read and understand. If possible, try to edit it and make it clearer.
I’m not sure, but it sounds like you want to use list comprehensions:
would generate: