Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this questionurlReq = urllib2.Request(theurl)
urlReq.add_header('User-Agent',random.choice(agents))
urlResponse = urllib2.urlopen(urlReq)
htmlSource = urlResponse.read()
How do I make htmlSource in 1 line, instead of many lines?
You can't really do that, the only possible thing is put the response and the source on the same line. Or you could use ;
between statements, but that's ugly.
But more importantly, why would you do that? Why is it better to have it all in on line?
>>> import this
The Zen of Python, by Tim Peters
...
Readability counts.
...
How to do that in one line? That's what functions are for. Like this:
def getsource(url):
urlReq = urllib2.Request(url)
urlReq.add_header('User-Agent',random.choice(agents))
urlResponse = urllib2.urlopen(urlReq)
return urlResponse.read()
Now you can do it in one line:
htmlSource = getsource(theurl)
Done!
Update:
Filtering the htmlSource to be one linebreak (as you now claim you want) is done something like this:
htmlSource = htmlSource.replace('\n', '')
And you might need
htmlSource = htmlSource.replace('\r', '')
as well. I sincerely doubt it will speed anything up.
精彩评论