Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a web scraping application that contains long string literals for the URLs. What would be the best way to present them (keeping in mind that I would like to adhere to PEP-8.

URL = "https://www.targetwebsite.co.foo/bar-app/abc/hello/world/AndThen?whatever=123&this=456&theother=789&youget=the_idea"
br = mechanize.Browser()
br.open(URL)

I had thought to do this:

URL_BASE = "https://www.targetwebsite.co.foo/"
URL_SUFFIX = "bar-app/abc/hello/world/AndThen"
URL_ARGUMENTS = "?whatever=123&this=456&theother=789&youget=the_idea"
br = mechanize.Browser()
br.open(URL_BASE + URL_SUFFIX + URL_ARGUMENTS)

But so many lines la! Also, not a standard way of representing a URL.

share|improve this question
add comment

2 Answers

You could use continuation lines with \, but it messes the indentation:

URL = 'https://www.targetwebsite.co.foo/\
bar-app/abc/hello/world/AndThen\
?whatever=123&this=456&theother=789&youget=the_idea'

Or you could use the fact that string literals next to each other are automatically concatenated, in either of these two forms:

URL = ('https://www.targetwebsite.co.foo/'
       'bar-app/abc/hello/world/AndThen'
       '?whatever=123&this=456&theother=789&youget=the_idea')

URL = 'https://www.targetwebsite.co.foo/' \
      'bar-app/abc/hello/world/AndThen' \
      '?whatever=123&this=456&theother=789&youget=the_idea'

I often use the parenthesized version, but the backslashed one probably looks cleaner.

share|improve this answer
add comment

I would put the URLS in a config file, or take them as a parameter.

share|improve this answer
add comment

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.