content = etree.HTML(text) h = content.xpath('//h1') h1 =
h[0].xpath('string(.)').strip()
实例测试:

测试网页地址:测试地址
<https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E5%9C%B0%E9%9C%87%E5%B1%80%E9%83%91%E5%B7%9E%E5%9F%BA%E7%A1%80%E5%B7%A5%E7%A8%8B%E5%8B%98%E5%AF%9F%E7%A0%94%E7%A9%B6%E9%99%A2%E6%A1%A9%E5%9F%BA%E6%A3%80%E6%B5%8B%E4%B8%AD%E5%BF%83>

代码如下:
import requests import re import pymysql from lxml import etree from pymongo
import MongoClient sessions = requests.session() sessions.headers['User-Agent']
= 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/27.0.1453.94 Safari/537.36' r = sessions.get(
'https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E5%9C%B0%E9%9C%87%E5%B1%80%E9%83%91%E5%B7%9E%E5%9F%BA%E7%A1%80%E5%B7%A5%E7%A8%8B%E5%8B%98%E5%AF%9F%E7%A0%94%E7%A9%B6%E9%99%A2%E6%A1%A9%E5%9F%BA%E6%A3%80%E6%B5%8B%E4%B8%AD%E5%BF%83')
r.encoding = 'utf-8' text = r.text content = etree.HTML(text) h =
content.xpath('//h1') h1 = h[0].xpath('string(.)').strip() d =
content.xpath("//div[@label-module='lemmaSummary']") d1 =
d[0].xpath('string(.)').strip() print(h1) print(d1)
结果如下: