最简单爬虫爬取小说内容

下面以一个爬取25章的小说内容 为例,存取到txt文档中和mysql数据库中。

import requests
import bs4
import pymysql

url='http://www.xbiquge.la/13/13959/'
res1=requests.get(url=url)
res1.encoding='utf-8'
# print(res1.text)  #html 文本文件
bs=bs4.BeautifulSoup(res1.text,'html.parser')
xs_name=bs.h1.string
# 开启数据库
conn=pymysql.connect(host='localhost',
                       db='xiaoshuo',
                       user='root',
                       password='123456',
                       charset='utf8')
cursor=conn.cursor()
sql='insert into xs(xs_name,xs_content,xs_text) values(%s,%s,%s)'
n=0
for dd in bs.find_all('dd'): #find_all('dd')拿到的是多有的dd标签  dd变量每一次循环,都是一个dd标签
    n+=1
    link='http://www.xbiquge.la'+dd.a['href'] #获取某一个dd中的a标签
    xs_content=dd.a.string  # 拿当前标签的内容
    # print(dd.a.text)    # 拿所有内容
    res2=requests.get(url=link)
    res2.encoding='utf-8'
    with open('aaa.txt','w',encoding='utf-8') as f:
        f.write(res2.text)
    with open('aaa.txt','r',encoding='utf-8') as f:
        text=f.read()
    bs2=bs4.BeautifulSoup(text,'html.parser')
    #得到小说的具体内容
    try:
        xs_text=bs2.find_all('div',attrs={'id':'content'})[0].text
    except:
        xs_text=None
    print(xs_text)
    cursor.execute(sql,args=[xs_name,xs_content,xs_text])
    if n==25:
        break
conn.commit()
cursor.close()
conn.close()


实现结果如下:

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值