import requests
from bs4 import BeautifulSoup
f = open('sanguoyanyi.txt', 'w', encoding='utf-8')
main_url = 'http://www.shicimingju.com/book/sanguoyanyi.html'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0'}
main_text = requests.get(url=main_url, headers=headers).text
soup = BeautifulSoup(main_text, 'lxml')
a_list = soup.select('.book-mulu > ul > li > a') # 解析出每一章的url,返回一个列表
for a in a_list:
chapter_title = a.string
detail_url = 'http://www.shicimingju.com' + a['href']
detail_page_text = requests.get(detail_url, headers=headers).text
detail_soup = BeautifulSoup(detail_page_text, 'lxml')
content = detail_soup.find('div', class_='chapter_content').text
f.write(chapter_title + ':' + content + '\n')
使用bs4解析,爬取三国演义
最新推荐文章于 2023-04-02 20:22:30 发布