为什么爬取的内容保存为txt文本,文件里没有内容的?
代码如下:
import requests
from bs4 import BeautifulSoup
url = 'https://www.shicimingju.com/book/luotongsaobei.html'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69'
}
page = requests.get(url = url,headers = headers)
page.encoding = 'utf-8'
page_text = page.text
print(page_text)
soup = BeautifulSoup(page_text,'lxml')
li_list = soup.select('.book-mulu > ul > li')
fp = open('./luotongsaobei.txt', 'w' ,encoding= 'utf-8')
for li in li_list:
title = li.a.string
detail_url += 'http://www.shicimingju.com' + li.a['href']
detail_page_text = requests.get(url = detail_url, headers = headers).text
soup = BeautifulSoup(detail_page_text,"html.parser")
content = soup.find("div", class_="chapter_content").text.strip()
fp.write(title + '/n' + content + '/n')
print(title,'爬取成功')
fp.close()