7
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!

thirtyyuan
  • 603
  • 2
  • 8
  • 13
  • Does this answer your question? [TypeError: can't use a string pattern on a bytes-like object in re.findall()](https://stackoverflow.com/questions/31019854/typeerror-cant-use-a-string-pattern-on-a-bytes-like-object-in-re-findall) – wwii Dec 24 '19 at 05:02

2 Answers2

11

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

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
5

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

ashin
  • 51
  • 1
  • 1
  • This answer is especially useful compared to the accepted answer, when the same regex needs to be used on both strings and files (bytes-like objects). – caram Feb 05 '20 at 08:25