TypeError: Expected string or bytes-like object TypeError: Expected string or bytes-like object TypeError: Expected string or bytes-like object
It is generally caused by data type mismatch.
There are six standard data types in Python3:
Print (type(object)) to check the current data type, where object is the object to query.
First, there is a code that looks like this:
import re import requests from bs4 import BeautifulSoup import lxml #get the html data urlSave = "https://www.douban.com/people/yekingyan/statuses" req = requests.get(urlSave) soup = BeautifulSoup(req.text,'lxml') # After parsing beautifulsoup, get the required data times = soup.select('div.actions > span') says = soup.select('div.status-saying > blockquote')
And then I’m going to look at it and I’m going to get the data what is the numeric type
print('says:',type(says))
The result: Says: lt; class ‘list’>
This tells us that the data selected from beautifulSoup in soup.select() is of the list type.
Next, extract the data in the list separately
#Traversing the output for say in says: print(type(say))
Let’s see what type it is
The result: <<; class ‘bs4.element.Tag’> , different from the above six types
Beautiful Soup converts a complex HTML document into a complex tree structure, where each node is a Python object. All objects can be classified into four types:
TagNavigableStringBeautifulSoupComment
Use regular expressions directly to the data
for say in says: # Regular expressions to get the necessary data say = re.search('<p>(.*?)</p>',say)
There is an error
TypeError: expected string or bytes-like object
Therefore, before the regular expression, the problem is solved by converting the data type. As follows:
for say in says: # Convert the data type, otherwise an error will be reported say = str(say) # Regular expressions to get the necessary data say = re.search('<p>(.*?)</p>',say)
Read More:
- Python PIP TypeError: expected str, bytes or os.PathLike object, not int
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable
- [Solved] pycocotools Install Error: ERROR: Error expected str, bytes or os.PathLike object, not NoneType while executing
- Python TypeError: coercing to Unicode: need string or buffer, NoneType found
- [Solved] Python3.9 Pycropto RSA Error: TypeError: can’t concat str to bytes
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- Python TypeError: not all arguments converted during string formatting [Solved]
- [Solved] error: when using the property decorator in Python, an error occurs: typeerror: descriptor ‘setter’ requires a ‘property’ object but
- [How to Solve] Python TypeError: ‘int‘ object is not subscriptable
- Python scatter chart error: TypeError: object of type ‘NoneType’ has no len()
- [Solved] RuntimeError: unexpected EOF, expected 73963 more bytes. The file might be corrupted.
- [Solved] TypeError: not all arguments converted during string formatting
- Python Error: SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3:
- TypeError: Decimal type object is not a JSON serialization solution
- Django Issues: TypeError: “Settings” object is irreversible
- Python Pandas Typeerror: invalid type comparison
- [Solved] Python Error: IndentationError: expected an indented block
- How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’
- Tips for Python 3 string.punctuation