爬取小说总结
一、 准备工作
(1) requests安装
在cmd中,使用如下指令安装requests:
pip install requests
(2) beautifulsoup的安装
pip install beautifulsoup4
(3) lxml的安装
python3.7中lxml的安装
二、 爬取第一章
目标网站:https://www.biqukan.com/38_38836/497783246.html
import requests
from bs4 import BeautifulSoup
def getOnepage(url):
r = requests.get(url) #通过requests.get方法get请求页面
r.status_code #响应状态码
html = r.text #获取爬取下来的html元素
saveText('test', html)
def saveText(title, context): #将内容通过文件保存下来,方便查看
fp = open(title+'.txt', 'a', encoding='utf-8