0
点赞
收藏
分享

微信扫一扫

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


殊途同归

# -*- 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)
# 百度一下,你就知道



举报

相关推荐

0 条评论