-->

signed

QiShunwang

“诚信为本、客户至上”

题目9 (第二种方法)爬取某学校官网通告前三页爬虫代码

2021/6/8 23:23:01   来源:

import requests
from lxml import etree
url="http://www.jscvc.cn/242/list{}.htm"
headers="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41"
for i in range(1,4):
    resp=requests.get(url.format(i))
    resp.encoding="utf-8"
    page_text=etree.HTML(resp.text)
    li_list=page_text.xpath('//*[@id="wp_news_w9"]/a')
    for li in li_list:
        title=li.xpath("./span[1]/text()")
        data=li.xpath("./span[2]/text()")
        aa.append((title,data))
        with open("d.txt","a",encoding="utf-8") as f:
            f.write(title[0]+","+data[0]+"\n")