Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re.search().TypeError: cannot use a string pattern on a bytes-like object

import urllib.request
import re

f = urllib.request.urlopen('http://www.geekynu.cn/')
html = f.read()

title = re.search('<title>(.*?)</title>', html)
print(title)
#print(title.decode('utf-8')) //I had try to solve by this code.

[python 3.5] when I use re.search()to read the Web title,the error is hapen"TypeError: cannot use a string pattern on a bytes-like object", what should I do? THX!

like image 457
thirtyyuan Avatar asked Jun 09 '16 09:06

thirtyyuan


People also ask

Does regex work on bytes in Python?

This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str ) as well as 8-bit strings ( bytes ).

What is a bytes like object in Python?

In Python, a string object is a series of characters that make a string. In the same manner, a byte object is a sequence of bits/bytes that represent data. Strings are human-readable while bytes are computer-readable. Data is converted into byte form before it is stored on a computer.


2 Answers

re needs byte patterns (not string) to search bytes-like objects. Append a b to your search pattern like so: b'<title>(.*?)</title>'

like image 167
Moses Koledoye Avatar answered Nov 12 '22 14:11

Moses Koledoye


If you could add html=html.decode('utf-8'), I think it will be ok.

like image 27
ashin Avatar answered Nov 12 '22 13:11

ashin