import parsel
2.把要解析的数据转化为Selector对象selector = parsel.Selector(要解析的内容)
3.调用css函数输入解析规则定位class里的h1,并拿到它里面的内容:
selector.css('.bookname h1::text').get()
定位id,并拿到它里面的内容:
selector.css('#content::text').getall() #getall()返回的是一个列表
用CSS解析网页文本案例
import requestsimport parselurl = "https://www.biquge.biz/58_58780/161880.html"respond = requests.get(url).text#print(respond)selector = parsel.Selector(respond)novle = selector.css('.bookname h1::text').get()content = selector.css('#content::text').getall()content = 'n'.join(content)print(novle)print(content.encode('gbk','ignore').decode('gbk','ignore'))