我在一个B站视频里跟着敲爬小说的python代码,和他的一摸一样,但是我的一直在报错,网上也没有正确的解决方法,就问一下大家。
操作环境、软件版本等信息
问题相关代码
import requests
from lxml import etree
class Spider(object):
def start_request(self):
#1.请求拿到小说名创建文件夹
response = requests.get("https://www.qidian.com/all")
html = etree.HTML(response.text)
Bigtit_list = html.xpath('//<div[@class="book-mid-info"]/h2/a/text()')
Bigsrc_list = html.xpath('//<div[@class="book-mid-info"]/h2/a/@href')
print(Bigtit_list, Bigsrc_list)
spider = Spider()
spider.start_request()
运行结果及报错内容
C:\Users\acer\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/acer/PycharmProjects/pythonProject/有声小说网站/小说抓取.py
Traceback (most recent call last):
File "C:/Users/acer/PycharmProjects/pythonProject/有声小说网站/小说抓取.py", line 15, in <module>
spider.start_request()
File "C:/Users/acer/PycharmProjects/pythonProject/有声小说网站/小说抓取.py", line 9, in start_request
Bigtit_list = html.xpath('//<div[@class="book-mid-info"]/h2/a/text()')
File "src\lxml\etree.pyx", line 1597, in lxml.etree._Element.xpath
File "src\lxml\xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
File "src\lxml\xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid expression
Process finished with exit code 1
我的解答思路和尝试过的方法
我觉得问题主要是看lxml.etree.XPathEvalError: Invalid expression
但是我根本没有找到什么解决方法,欢迎帮忙解答一下