
说明
xpath是一种在XML文档中搜索信息的语言。
过程
1、获得浏览器中的标题和作者测试。
2、在浏览器安装xpath插件:
3、在html中找到book-mid-info:
4、找到我们想得到小说的名字、作者
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 作者:
# 开发时间:2021/4/8/0008 8:24
import requests
from lxml import etree
url= "https://www.qidian.com/rank/yuepiao"
headers={ 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3861.400 QQBrowser/10.7.4313.400' }
#发送请求
resp=requests.get(url,headers)
e=etree.HTML(resp.text) #类型转换,把str转变为class 'lxml.etree._ELement
print(type(e))
names=e.xpath('
authors=e.xpath('
print(names)
print(authors)
#名称和作者对应
for name,authors in zip(names,authors):
print(name, ":" ,authors)
|
以上就是python中使用XPath爬取小说的方法,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。