Python编程:BeautifulSoup和Selector解析网页示例

版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/88112961

殊途同归

# -*- coding: utf-8 -*-

from bs4 import BeautifulSoup
import requests
from parsel import Selector

# 下载网页
url = "https://www.baidu.com/"
response = requests.get(url)
response.encoding = response.apparent_encoding

# BeautifulSoup解析网页
soup = BeautifulSoup(response.text, "html.parser")
title = soup.find("title")
print(title.text)
# 百度一下,你就知道

# Selector解析网页
sel = Selector(text=response.text)
title = sel.css("title::text").extract_first()
print(title)
# 百度一下,你就知道

没有更多推荐了,返回首页