python爬虫题目

网站

https://project-iprj6705f17ebcfad66461658c5c-8000.preview.node01.inscode.run/

第一道题爬取api并且保存

python 复制代码
import requests,re
import json
url = "https://project-iprj6705f17ebcfad66461658c5c-8000.preview.node01.inscode.run/tasks/api/"
headers= {

'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
}

res = requests.get(url,headers=headers).json()
with open('1.json','w') as f:
    f.write(json.dumps(res,ensure_ascii=False))

第二道爬取所有图片

python 复制代码
from urllib.parse import urljoin
import requests,re
from urllib.parse import urlparse
import json
url = "https://project-iprj6705f17ebcfad66461658c5c-8000.preview.node01.inscode.run/tasks/api/"
headers= {

'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
}

res = requests.get(url,headers=headers).json()
list1 = res['articles']
list2=[]
for i in list1:
    list2.append(i['image'])
base_url ="https://"+urlparse(url).netloc

for image in list2:
    image_url = urljoin(base_url,image)
    img = requests.get(image_url).content
    img_name = image.split("/")[-1]
    with open(img_name,'wb') as f:
        f.write(img)

第三道 爬取题目和摘要

python 复制代码
import requests,csv
from lxml import etree
with open("data.csv","w",newline='',encoding='gbk') as f:
    writer = csv.writer(f)
    writer.writerow(["题目","再要"])
url = "https://project-iprj6705f17ebcfad66461658c5c-8000.preview.node01.inscode.run/tasks/article/list/"
headers= {

'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36'
}

res = requests.get(url,headers=headers)
html = etree.HTML(res.text)
wen_zhang = html.xpath('//div[@class="lab-block"]//a//@href')
with open("data.csv","w",newline='',encoding='gbk') as f:
    writer = csv.writer(f)
    writer.writerow(["ti","zai"])



for i in wen_zhang:
    url_l = "https://project-iprj6705f17ebcfad66461658c5c-8000.preview.node01.inscode.run/"+i
    result = requests.get(url_l,headers=headers)
    select = etree.HTML(result.text)
    timu = select.xpath('//h2/text()')[0]
    zaiyao = select.xpath('//p//text()')
    result = "".join(zaiyao)
    with open("data.csv", "a", newline='',encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow([timu, result])
相关推荐
华科云商xiao徐17 分钟前
Node.js浏览器引擎+Python大脑的智能爬虫系统
爬虫·python·node.js
猿榜1 小时前
Python基础-Python简介
python
这里有鱼汤1 小时前
亲测可行!Streamlit项目完美打包成EXE分享教程(含xtquant坑点)
后端·python
侧耳倾听1112 小时前
Java的异常机制
java·开发语言
草莓熊Lotso2 小时前
《吃透 C++ 类和对象(上):封装、实例化与 this 指针详解》
开发语言·c++·经验分享·笔记·其他
crushqqi2 小时前
【跨服务器的数据自动化下载--安装公钥,免密下载】
服务器·python·自动化
qq_463944862 小时前
如何将新建的Anaconda虚拟环境导入Juputer内核中?
linux·windows·python
Shun_Tianyou2 小时前
Python Day28 HTML 与 CSS 核心知识点 及例题分析
开发语言·前端·css·python·算法·html
·前路漫漫亦灿灿2 小时前
C++11-下
开发语言·c++
zhong liu bin3 小时前
JVM基础【Java】
java·开发语言·jvm·intellij-idea