爬取第一试卷网高三数学试卷并下载到本地

python 复制代码
import requests
import re
import os
filename = '试卷\\'
if not os.path.exists(filename):
    os.mkdir(filename)
url = 'https://www.shijuan1.com/a/sjsxg3/list_727_1.html'
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
response = requests.get(url=url,headers=headers)
response.encoding = response.apparent_encoding
href_list = re.findall("<td width='52%' height='23'><a href=\"(.*?)\" class=\"title\" target='_blank'>",response.text)
title_list = re.findall("class=\"title\" target='_blank'>(.*?)</a>",response.text)
# https://www.shijuan1.com/a/sjywg3/243565.html
for title,href in zip(title_list,href_list):
    href = 'https://www.shijuan1.com'+href
    data_html = requests.get(url=href,headers=headers)
    data_html.encoding = data_html.apparent_encoding
    data_url = 'https://www.shijuan1.com'+re.findall('<li><a href="(.*?)" target="_blank">本地下载</a></li>',data_html.text)[0]
    doc = requests.get(url=data_url,headers=headers).content
    with open('试卷\\'+title+'.rar',mode='wb') as f:
        f.write(doc)

结果展现:

改进代码:

python 复制代码
import requests
import os
import re

def get_html_data(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    response = requests.get(url=url,headers=headers)
    response.encoding = response.apparent_encoding

    return response

def get_analyse_html(response):
    href_list = re.findall("<td width='52%' height='23'><a href=\"(.*?)\" class=\"title\" target='_blank'>",
                           response.text)
    title_list = re.findall("class=\"title\" target='_blank'>(.*?)</a>", response.text)

    return title_list,href_list

def save(title_list,doc_list):
    filename = '试卷\\'
    if not os.path.exists(filename):
        os.mkdir(filename)
    for title,doc in zip(title_list,doc_list):
        with open('试卷\\' + title + '.rar', mode='wb') as f:
            f.write(doc)
            print(f'{title}已经下载完成')

def get_doc(href_list):
    doc_list = []
    for href in  href_list:
        href = 'https://www.shijuan1.com' + href
        doc_html = get_html_data(href)
        data_url = 'https://www.shijuan1.com' + re.findall('<li><a href="(.*?)" target="_blank">本地下载</a></li>', doc_html.text)[0]
        doc = get_html_data(data_url).content
        doc_list.append(doc)
    return doc_list


if __name__ == '__main__':
    url = 'https://www.shijuan1.com/a/sjsxg3/list_727_1.html'
    response = get_html_data(url)
    title_list,href_list = get_analyse_html(response)
    doc_list = get_doc(href_list)
    save(title_list,doc_list)

进一步写成类:

python 复制代码
import requests
import os
import re
class save_doc():


    def get_html_data(self,href):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
        }
        response = requests.get(url=href, headers=headers)
        response.encoding = response.apparent_encoding

        return response

    def get_analyse_html(self,response):
        href_list = re.findall("<td width='52%' height='23'><a href=\"(.*?)\" class=\"title\" target='_blank'>",
                               response.text)
        title_list = re.findall("class=\"title\" target='_blank'>(.*?)</a>", response.text)

        return title_list, href_list

    def save(self,title_list,doc_list):
        filename = '试卷\\'
        if not os.path.exists(filename):
            os.mkdir(filename)
        for title, doc in zip(title_list, doc_list):
            with open('试卷\\' + title + '.rar', mode='wb') as f:
                f.write(doc)
                print(f'{title}已经下载完成')

    def get_doc(self,href_list):
        doc_list = []
        for href in href_list:
            href = 'https://www.shijuan1.com' + href
            doc_html = self.get_html_data(href)
            data_url = 'https://www.shijuan1.com' + re.findall('<li><a href="(.*?)" target="_blank">本地下载</a></li>', doc_html.text)[0]
            doc = self.get_html_data(data_url).content
            doc_list.append(doc)
        return doc_list
save = save_doc()
response = save.get_html_data('https://www.shijuan1.com/a/sjsxg3/list_727_1.html')
title_list,href_list = save.get_analyse_html(response)
doc_list = save.get_doc(href_list)
save.save(title_list,doc_list)

对于类还是很不熟,我想要类中的方法返回的值,可以直接传入类中的其他方法,应该怎么写呢?我想要写一个类,传入一个url,直接下载所需要的数据,即最终代码为

python 复制代码
save = save_doc("https://www.shijuan1.com/a/sjsxg3/list_727_1.html")

不需要上面那么复杂的传来传去,应该怎么做呢?

相关推荐
天启HTTP9 小时前
爬虫返回403错误?从请求特征到网络环境逐步排查
网络·爬虫·tcp/ip
钱栈up1 天前
长安汽车把海外年销钉在100万辆,2030年要翻到200万——出海从故事变成基建
爬虫·python·数据分析
李松桃3 天前
Python爬虫基础
开发语言·爬虫·python·get请求使用方法·post请求使用方法
步峰代理-盾山5 天前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
钱栈up5 天前
三星盯上散热老玩家Modine:AI算力链最后一块拼图要进自家口袋
爬虫·python·数据分析
tryCbest6 天前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
狗都不学爬虫_7 天前
App逆向 - 希音 - 无感与图标验证码纯算
爬虫·python·网络爬虫
钱栈up7 天前
两台机器、一个 AI 记忆库:101 秒把 137 个文件对齐
爬虫·python·数据分析
钱栈up7 天前
外网通、QQ 登不上:一条 workbuddy 脚本塞进去的路由三天没还回来
爬虫·python·数据分析
choicesages8 天前
跑通不等于跑对:公开评论抓取里三处不报错的坐标问题
爬虫·python·数据清洗·数据质量·去重