Search code examples
pythontwitterpython-module

How to fix twint error "CRITICAL:root:twint.get:User:replace() argument 2 must be str, not None"


I'm trying to use the twint module to get some information from twitter, in particular the bio. The code example works just fine:

import twint

c = twint.Config()
c.Username = "twitter"

twint.run.Lookup(c)

yields

783214 | Twitter | @Twitter | Private: 0 | Verified: 1 | Bio: What’s happening?! | Location: Everywhere | Url: https://about.twitter.com/ | Joined: 20 Feb 2007 6:35 AM | Tweets: 10816 | Following: 140 | Followers: 56328970 | Likes: 5960 | Media: 1932 | Avatar: https://pbs.twimg.com/profile_images/1111729635610382336/_65QFl7B_400x400.png

Thing is, I only need the bio data. According to the site, you can use

c.Format = 'bio: {bio}'

Unfortunately, this yields

CRITICAL:root:twint.get:User:replace() argument 2 must be str, not None

I think this may be due to the following code line (from here):

output += output.replace("{bio}", u.bio)

Where the u.bio value is assigned here:

u.bio = card(ur, "bio")

The card function does the following when our type is "bio":

if _type == "bio":
    try:
        ret = ur.find("p", "ProfileHeaderCard-bio u-dir").text.replace("\n", " ")
    except:
        ret = None

I think the problem may lie in the second part, where a value is assigned to u.bio, either not even being called or returning None for some reason. Unfortunately, I do not know how to fix that or call the function.

I've had a similar problem before with a different function, twint.run.Following(c), but was able to solve it by not setting c.User_full = true

Could anyone help me out?


Solution

  • The format should be of the form

    c.Format = "{bio}"
    

    If you wanted multiple fields

    c.Format = "{bio} | {name}"
    

    I find you get a rate limit of 250 items before a blocker drops down and you need to wait for a few minutes for it to lift.