刚学python, 我参考网上的文章,自己写了一个爬电子书网站的程序,获取到respone后,输出的内容是OK的。但是想获取具体的内容的时候却一直是空,不知道问题出在哪儿?求指导,谢谢
首先,网页的结构是这样的
所以我写的内容如下
responseContent = requests.get(realUrl, headers)
if responseContent.status_code != 200:
print("Get content error!")
responseContent.encoding = 'utf-8'
pageTextContent = responseContent.text
txtFile = open('D:\\code\\python\\txtbook\\text_all.txt', 'w+')
txtFile.write(pageTextContent)
txtFile.close()
mTreeContent = etree.HTML(pageTextContent)
chapterTitle = mTreeContent.xpath("//h1[@class='title']/text()")[0]
print(chapterTitle)
chapterTexts = mTreeContent.xpath("//div[@class='content']/text()")
txtFile = open('D:\\code\\python\\txtbook\\text.txt', 'w+')
txtFile.write(chapterTexts[0])
txtFile.close()
在输出全网页内容的时候,显示是正常的
而在获取content的内容的时候,却获取的是空白
各种尝试,各种改,都没有能解决问题,求解我遇到的问题是出在哪儿了吗?
chapterTexts = mTreeContent.xpath("//div[@id='content']/text()")
试一试。