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

2、python之爬虫css属性解析以及2级爬取

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

2、python之爬虫css属性解析以及2级爬取

文章目录
  • 欢迎使用PyChram编辑器

欢迎使用PyChram编辑器
import pathlib
import uuid
from requests import get
from scrapy.selector import Selector


urlHttp = "https://www.51moot.net"
count = 0
for index_page in range(0, 5):
    html = get(
        "https://www.51moot.net/main/course?search_id=0&is_free=-1&page_index={0}".format(index_page)).content.decode(
        "utf-8")
    sel = Selector(text=html)
    result = sel.css("div.course-details-cont-view ul li a::attr(href)").extract()
    for x in result:
        x = urlHttp + x
        html = get(x).content.decode("utf-8")
        sel = Selector(text=html)
        strInfo = ""
        title = sel.css("div.course-details-title-cont-text h2::text").extract()[0]
        infos = sel.css(
            "div.course-details-title-cont-text li.course-details-title-cont-text-chapter span::text").extract()
        introduce = sel.css(
            "div.course-details-view-list div.course-details-view-list-introduce-cont p::text").extract()[0]
        strInfo += title + "n"
        strInfo += "主讲人:{0}t章节数:{1}t学习时长:{2}课时t学习人数:{3}人n".format(infos[0], infos[1], infos[2], infos[3])
        strInfo += "课程简介:{0}".format(introduce)
        try:
            saveUrl = str.format("D:/save51Moot/{0}{1}", title, ".txt")
            path = pathlib.Path(saveUrl)
            if path.exists():
                saveUrl = str.format("D:/save51Moot/{0}{1}{2}", title, uuid.uuid4(), ".txt")
            file = open(saveUrl, "w", encoding="utf-8")
            file.write(strInfo)
            file.close()
            count += 1
        except:
            print("异常链接:", x)
print("共计保存文件:", count, "个")


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

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

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