开发者

I do urllib2 and I download the htmlSource of the webpage. How do I make this all on 1 line? [closed]

开发者 https://www.devze.com 2022-12-08 19:20 出处:网络
Closed开发者_开发问答. This question needs details or clarity. It is not currently accepting answers.
Closed开发者_开发问答. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 2 years ago.

Improve this question
urlReq = 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消