Get Sina Weibo cookies

Source: Internet
Author: User

Baidu has also been a bit earlier how to get Sina Weibo cookies, with the most frequently seen tutorial URLs https://www.douban.com/note/264976536/?start=0#32893498

You follow the above steps to try, and then found that weibo.com can not find, and then try to use the other mobile Weibo will be able to www.weibo.cn

First also open with Chrome browser, F12 open (my keyboard does not seem to convert, so use fn+f12), or right-click Check

Other.. As pictured, find and then copy the long list of cookies

In order to verify, I tried this by the way. Example of submitting a cookie as a header parameter for accessing a microblog https://gist.github.com/ghostrong/d10c061000b7b65e5039

Attach source code

#coding =GBK ""


simulate a user login to Sina Weibo with cookie.
You can use the This method to visit any page that requires login. ""


Import urllib2
import re


cookie = ' Your cookie '  # get your cookie from Chrome or Firefox
headers = {
    ' User-agent ': ' mozilla/5.0 (Windows NT 6.1; rv:24.0) gecko/20100101 firefox/24.0 ',
    ' cookie ': Cookie
}


def Visit ():
    url = ' http://weibo.com '
    req = urllib2. Request (URL, headers=headers)
    text = Urllib2.urlopen (req). Read ()

    # Print the title, check if you login to Weibo sucessfully
    pat_title = Re.compile (' <title> (. +?) </title> ')
    r = pat_title.search (text)
    if r:
        print R.group (1). Decode ("Utf-8")


if __name__ = = ' __main__ ':
    visit ()

The first line of the original code is defined as #coding =utf-8

But I have been running the problem of output garbled, and then checked the information, attached link http://www.cnblogs.com/FlyCat/archive/2013/04/06/3002885.html

The reason for garbled is that Python reads the default decoding method is the operating system code, if and when the encoding method is not the same, there will be garbled

For example, the following fragment, file Save format is Utf-8

#coding =utf-8
print ' is ' #输出乱码

Because the default encoding for Windows is the Gbk,python file is saved with the Utf-8, when read, Python uses the GBK encoding table to solve UTF-8 encoded bytecode, because GBK and UTF-8 encoding is incompatible, there is a natural garbled problem

Workaround:

1. Direct use u ' is ' form, indicated in Unicode encoding, decoding will be at the top of the #coding定义的编码方式, if not written to the operating system current encoding method, it is recommended to write on #coding, because to make the operating system encoding and source file encoding often will be different. It is recommended to use this method

2. Specify the decoding method at output print ' is '. Decode ("UTF8"), must be consistent with the saved encoding, ignoring the definition of #coding

3. Change the #coding and save encoding to the same as the operating system encoding, you can directly print ' is ' normal output, is not recommended, because you need to know the operating system code, copied to other computers, the operating system code is not the same error

#coding =gbk
print u ' is ' #方法1
print ' is '. Decode ("GBK") #方法2
print ' is ' #方法3

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.