![540641c88aabaaab17277e763b6aa93b.png](https://i-blog.csdnimg.cn/blog_migrate/ecc64037fc217d5da82eeed4e4175dc3.jpeg)
通过bs4模块爬取小说。
注意:bs4只能用在python中
'''
古诗词网爬取《西游记》
bs4这种解析方式只能用在python中
实例化soup对象的两种方法:
1. 本地文件
fp=open(' ','lxml') 把赋值成功的fp扔进汤里
soup=BeautifulSoup(fp,'html)
2. 网络文件:
soup = BeautifulSoup(page_text, 'lxml')
'''
from bs4 import BeautifulSoup
import requests
# 要爬取的小说主页
url='https://www.shicimingju.com/book/xiyouji.html'
headers={
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36' }
# 用.content.decode()方法,用.text属性可能出现乱码
page_text=requests.get(url,headers=headers).content.decode()
# 通过美丽汤来解析主页里【每