AI智能问答-学无止境 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)
上一篇
下一篇

发表回复

返回顶部