python网页解析:BeautifulSoup与Selector

  开发

5fcd8d4f86040755.jpg_fo742.jpg

from bs4 import BeautifulSoup
import requests
from parsel import Selector

# requests.get(url)
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)

LEAVE A COMMENT

Captcha Code