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

爬取某某大学新闻首页前三页以csv的模式保存

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

爬取某某大学新闻首页前三页以csv的模式保存

import requests
import lxml.html as lh
import csv
headers ={
"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 x-requested-with: XMLHttpRequest"
}
for i in range(3):
    url = f'https://www.ahu.edu.cn/15129/list{i+1}.htm'
    response = requests.get(url,headers=headers)
    response.encoding="utf-8"
    root = lh.fromstring(response.text)
    notices = root.xpath("//div/div[@id='wp_news_w25']/ul/li")
    for notice in notices:
        #标题
        title = notice.xpath(".//span[1]/a/text()")[0]
        # 日期
        date = notice.xpath(".//span[2]/text()")[0]
        print(title,date)
        with open('data.csv', 'a',encoding="utf-8") as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow([title,date])
       以上内容为新学爬虫测试专用如果侵权请提示删除谢谢

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

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

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