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)