使用requests库获取网页内容,然后使用bs4库解析网页,最后使用Xpath定位元素并提取其内容。
示例代码:
import requestsfrom bs4 import BeautifulSoup
url = 'http://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml')
# 使用Xpath定位元素并提取其内容
result = soup.xpath('//div[@class="example"]/p/text()')
print(resu