The difference between crawler content and text in Python

Source: Internet
Author: User

I've been thinking about the difference between the content and the Text property of requests, which is no different from the print results.

Importrequestsheaders= {    "user-agent":"mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) applewebkit/537.36 (khtml, like Gecko) chrome/65.0.3325.181 safari/537.36"}url='https://www.sogou.com/web?query={}'Key= Input ('Please enter') Params= {'Query': Key}response= Requests.get (url,params=params,headers=headers)Print(Response.text)

The conclusion is that text returns a Unicode type of data, typically a coded form defined in the header of a Web page.

The content returned is bytes, the second-class data.

Which means if you want to extract the text, use text.

But if you want to extract pictures and files, you need content.

  #1. Import the Requests module    ImportRequests#2. Send Request for Binary data (bytes)Respone = Requests.get ("http://imgsrc.baidu.com/image/c0%3Dpixel_huitu%2C0%2C0%2C294%2C40/sign=098c3f828cd6277ffd1f3a7841407a5c/ 3c6d55fbb2fb4316e3afd1432ba4462309f7d353.jpg")    #get binary data; Note: Here do not decode, because pictures, videos and other files are binary not text, do not need to decode    #data = Respone.content    #3. Writing data to a fileWith open ("wallpapers. jpg","WB") as F:f.write (respone.content)

The difference between crawler content and text in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.