PythonStudy_爬取网页title和摘要


# coding=utf-8
import re
from urllib.request import urlopen
from bs4 import BeautifulSoup

# 获取网页标题
def get_url_Title_Description(url):
    # 获取网页全部信息content
    content = urlopen(url).read().decode('utf-8')

    # 正则表达式匹配标题
    pat = r'<title>(.*?)</title>'
    title = re.findall(pat,content)

    # 提取网页摘要
    soup = BeautifulSoup(content,"html.parser")
    description = soup.find(attrs={"name":"description"})['content']

    # 返回标题和摘要
    return (title[0],description)

# ----------test----------------
# url = "http://www.sina.com.cn/"
# title,dsp = get_url_Title_Description(url)
# print(title)
# print(dsp)

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM