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

本文介绍了如何使用Python的requests库进行网页抓取,从指定URL获取资源链接,然后下载并打包为RAR文件。作者还展示了如何将这些操作封装到一个名为`save_doc`的类中,简化调用过程。
摘要由CSDN通过智能技术生成
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)

结果展现:

改进代码:

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)

进一步写成类:

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,直接下载所需要的数据,即最终代码为

save = save_doc("https://www.shijuan1.com/a/sjsxg3/list_727_1.html")

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

  • 12
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果你要的是免费的音乐资源,可以使用Python的虫库来实现。一般情况下,音乐资源的流程如下: 1. 分析音乐资源的站结构,找到音乐资源的URL。 2. 使用Python的requests库向服务器发送请求,获音乐资源的HTML数据。 3. 使用正则表达式或BeautifulSoup库等工具,解析音乐资源的HTML数据,提音乐资源的下载链接。 4. 使用Python的requests库再次向服务器发送请求,下载音乐资源并保存到本地。 以下是一个简单的示例代码,以下载某个站上的免费音乐为例: ```python import requests import re # 1. 分析音乐资源的站结构,找到音乐资源的URL url = 'http://example.com/music' # 2. 使用Python的requests库向服务器发送请求,获音乐资源的HTML数据 response = requests.get(url) # 3. 使用正则表达式或BeautifulSoup库等工具,解析音乐资源的HTML数据,提音乐资源的下载链接 download_links = re.findall(r'<a href="(.*\.mp3)">', response.text) # 4. 使用Python的requests库再次向服务器发送请求,下载音乐资源并保存到本地 for link in download_links: filename = link.split('/')[-1] with open(filename, 'wb') as file: file.write(requests.get(link).content) ``` 需要注意的是,如果要的音乐资源需要付费或需要登录才能下载,就需要使用相应的技术手段进行处理。同时,音乐资源也需要遵守相关法律法规和道德规范,不得侵犯他人的合法权益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力学习各种软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值