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!
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 ).
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.
re
needs byte patterns (not string) to search bytes-like objects. Append a b
to your search pattern like so: b'<title>(.*?)</title>'
If you could add html=html.decode('utf-8')
, I think it will be ok.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With