2tu3-学无止境 python Python3 截取html之正则表达re.compile()

Python3 截取html之正则表达re.compile()

import re,requests
def parse_response(url):
header = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/63.0.3239.132 Safari/537.36 '
}
response = requests.get(url, headers=header, timeout=2)
response.encoding = 'UTF-8'
html = response.text
pattern = re.compile('<title>(.*?)</title>', re.S)
items = re.findall(pattern, html)
print(items[0])

return items

if __name__ == "__main__":
url=input('输入网址:')
parse_response(url)
本文来自网络,不代表2tu3-学无止境立场,转载请注明出处:https://2tu3.com/biji/167/

作者: 热心网友

上一篇
下一篇
返回顶部