Python_itchat,get user information and process it to get each Value__python

Source: Internet
Author: User

Don't say much, come directly to the code

# lead Itchat
import itchat

# WeChat login
itchat.login ()
# Get all users (may be all, print out too messy to see)
user = Itchat.get_ Friends ()

The initial operation is not complicated, and a few lines of code get all the users
But all users can not be used directly, need to get the specified value

# I'm here to scan the user himself for example

# take out the first paragraph in user (that is, the user himself)
i = str (user[:1])
print (i)
# str (), because it has to be processed below (the length of I is 1 if not converted) , cannot be processed further)
# print (len (user[:1]))

# Just to eliminate some side corners, you can omit
info = i[8:-2]
print (info)

The above ' info ' can be omitted, why I have to write it.
Because I thought that after elimination can be converted to JSON format, are tears ...

loads (),eval () have tried, leave a hole, to pass by the teacher for advice.

Import re

# Use the regular method to extract value
# below explain this structure 
name = str (re.findall r "' Nickname ': ' (. +?) '", info)) [2:-2]
UIn = str (Re.findall (r "' UIn ': (. +?),", info)) [2:-2]
# print (name)
# print (UIn)

Re.findall (), parameters are regular expressions and processing strings
Regular format I don't have to say more.
r "' Nickname ': ' (. +?) '", processing ' nickname ': ' beginning and ' end of intermediate data
Converted to a string for slicing
Slicing is because re.findall () after processing will let value plus ['], I do not need this format (if necessary to adjust themselves)

Complete code presentation (for reference only):

Import itchat
import re

itchat.login ()

user = Itchat.get_friends ()

i = str (user[:1))
print (i)
info = i[8:-2]
print (info)

name = str (Re.findall (r "' Nickname ': ' (. +?) '", info)) [2:-2]
sign = str ( Re.findall (r "' Signature ': ' (. +?) '", info)) [2:-2]
sex = str (Re.findall (r "' Sex ': (. +?),", info)) [2:-2]
UIn = STR (Re.findall (r "' UIn ': (. +?),", info)) [2:-2] Print (name) print (sex) print (
UIn)



I hope I can help you solve your problems.
Come on, mutual encouragement.

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.