首页 > 其他分享 >爬虫实战-爬取小说信息

爬虫实战-爬取小说信息

时间:2022-11-30 16:45:18浏览次数:38  
标签:实战 w100 text getall 爬虫 li 爬取 小说 css

import requests
import parsel
import csv
for i in range(1,5):
    print(f"--------爬取第{i}页-----------")
    url = f'https://www.slyqw.com/sort/{i}'
    header = {
            'User-Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 107.0.0.0Safari / 537.36',
            'Cookie': 'zh_choose = s;Hm_lvt_c4eec4b108dac241786b4218f0d27642 = 1669790844;Hm_lpvt_c4eec4b108dac241786b4218f0d27642 = 1669791342',
            'Referer': 'https: // www.slyqw.com / whole.html'
            }
        # 通过requests 模拟发送请求
    r = requests.get(url=url,headers=header)
    response = r.text
        # 引入pansel模块
    response = parsel.Selector(response)
    lis = response.css('.flex li')
    for li in lis:
        leibie = li.css('.img_span span::text').getall() # 小说类别
        title = li.css('.w100 a h2::text').getall()  # 小说标题
        zuozhe = li.css('.w100 div i::text').getall()  # 小说作者
        zishu = li.css('.w100 div .orange::text').getall()  # 小说字数
        shijian = li.css('.w100 div .blue::text').getall()  # 小说发布时间
        jieshao = li.css('.w100 p::text').getall()  # 小说简介
        with open('小说连载.csv',mode='a',encoding='utf-8-sig',newline='') as f:   # 这里的newline='' 如果不加,会出现跳行空行 
            writefile = csv.writer(f)
            writefile.writerow([title,zuozhe,zishu,shijian,leibie,jieshao])
    print("完成!")

  以上信息是爬取 5 页小说的信息。当然爬取信息之后需要在excel里面转换替换一下不要的字符,这里也可以在代码里面直接写  .replace()方法替换,我是直接用的excel的 ctrl+H  进行替换的。

 

 

标签:实战,w100,text,getall,爬虫,li,爬取,小说,css
From: https://www.cnblogs.com/frank1/p/16938899.html

相关文章