栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

豆瓣爬虫练习

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

豆瓣爬虫练习

import csv
import os

from lxml import etree
import  requests

header={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}


def get(url):
    list = []
    res=requests.get(url,headers=header)
    html=etree.HTML(res.content)
    titles=html.xpath('//div[@]/a/@title')
    authors=html.xpath('//p[@]/text()')
    evaluates=html.xpath('//span[@]/text()')
    links=html.xpath('//a[@]/@href')
    jpgs=html.xpath('//a[@]/img/@src')
    for title ,author,evaluate,link ,jpg in zip(titles,authors,evaluates,links,jpgs):
        dic = {}
        e=evaluate
        e=e.replace('n','').replace(" ",'')
        dic['书名']=title
        dic['作者']=author
        dic['评论数']=e
        dic['链接'] = link
        dic['图片地址']=jpg
        list.append(dic)
        os.makedirs("C:\豆瓣\"+title)
        pic=requests.get(jpg,headers=header)
        res=requests.get(link,headers=header)
        html=etree.HTML(res.text)
        jianjie = html.xpath('//div[@]//text()')
        print(list)
        with open("C:\豆瓣\"+title+"\"+title+'.jpg', 'ab') as f:

            for chunk in pic.iter_content(chunk_size=1000):
                if chunk:
                    f.write(chunk)
        with open("C:\豆瓣\"+title+"\"+title+'.txt', 'a',encoding='utf-8') as f:
            try:
                f.write(jianjie[1])
            except UnicodeEncodeError as e:
                pass
        with open("douban.csv", 'w', newline="") as f:
            file = csv.writer(f)
            for cow in list:
                file.writerow(cow)
    return list


for n in range(0,25,25):
    url="https://book.douban.com/top250?start="+str(n)

    a=get(url)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号