Python爬取前五页内容并保存访问每个视频内容数据-保存文件夹

import requests
from lxml import etree

def text_create(name, msg):
    try:
        desktop_path = "D:\\test\\"  # 新创建的txt文件的存放路径
        full_path = desktop_path + str(name) + '.txt'  # 也可以创建一个.doc的word文档
        file = open(full_path, 'w')
        file.write(msg)
    except Exception as E_results:
        print(E_results)

def title(headers,url,x):

    response = requests.get(url, headers=headers)
    text = response.text
    html = etree.HTML(text)

    # 获取标题
    titles = html.xpath("//div[@class='course-details-title-cont-text']//ul//li//h2/text()")[0].replace('\t','')+str(x)

    # 获取主讲人
    pepoe = html.xpath("//div[@class='course-details-title-cont-text']//ul//li//p//span[1]//text()")
    class_test = html.xpath("//div[@class='course-details-view-list-introduce-cont']//p[@class='middleColor']/text()")[0]
    test_txt="标题\n"+str(titles)+"主讲人\n"+str(pepoe[0])+"章节数\n"+str(pepoe[1])+"课时\n"+str(pepoe[2])+"学习人数\n"+str(pepoe[3])+"课程简介\n"+str(class_test)
    print(pepoe)
    text_create(titles, test_txt)
    #
    # if str(pepoe[1])=="0":
    #     print("文件异常过滤" +titles)
    #     return  1;
    #
    # else:
    #     text_create(titles,test_txt)
    #     return 0;

if __name__ == '__main__':
    numberss=0
    for i in range(0,5):

        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
        }
        url = "https://www.51moot.net/main/course?search_id=0&is_free=-1&page_index="+str(i)
        response = requests.get(url, headers=headers)
        text = response.text
        html = etree.HTML(text)
        new_url = html.xpath("//div[@class='course-details-cont-view']//ul//li//a//@href")

        for i in range(len(new_url)):
            title(headers, "https://www.51moot.net" + new_url[i],i)
            # temp=
            # numberss=numberss+int(temp)
# print(numberss)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫用于抓取网页数据,哔哩哔哩(B站)上的视频热门榜单可以通过网络请求获取其HTML内容,然后解析HTML结构来提取所需信息。以下是一个基本的步骤概述: 1. **库准备**:使用Python的requests库发送HTTP请求,获取B站的HTML面;BeautifulSoup或lxml库用于解析HTML。 ```python import requests from bs4 import BeautifulSoup ``` 2. **发送请求**: - 使用`requests.get()`获取B站的热门视频URL,通常这些数据会定期刷新,你可以先找到API接口,但公开的可能有限制。 3. **解析HTML**: - 使用BeautifulSoup解析HTML文档,查找包含视频信息的部分,如标题、链接、播放量等。 ```python soup = BeautifulSoup(response.text, 'html.parser') hot_videos = soup.find_all('div', class_='video-item') # 示例:假设热门视频在CSS类为'video-item'的元素中 ``` 4. **数据提取**: - 遍历解析出的HTML元素,提取每个视频的相关数据,例如使用`div['data-video']`获取视频ID。 5. **存储数据**: - 将数据存储到本地文件,列表、字典或其他数据结构中,或者直接上传到数据库。 ```python data = [] for video in hot_videos: video_id = video['data-video'] title = video.find('a')['title'] view_count = video.find('span', class_='stat-view').text data.append({'id': video_id, 'title': title, 'view_count': view_count}) ``` 6. **处理可能的问题**: - 考虑反爬策略,可能需要设置用户代理、处理验证码、限制请求频率等。 7. **错误处理**: - 添加适当的错误处理代码,比如处理网络错误、解析错误等。 ```python try: # 爬虫代码... except requests.exceptions.RequestException as e: print(f"请求失败: {e}") except Exception as e: print(f"解析失败: {e}") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值