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])
以上内容为新学爬虫测试专用如果侵权请提示删除谢谢



