小白一枚我不知道哪里错了
请各位大神们解决一下吧

代码附上

coding=gbk”

模拟访问页面

http的请求

常见的HTTPresquest方法:

GET请求——-大多数网页的访问

HTTP请求——用户需要传输数据给服务器 服务器接收数据以后处理完毕 返回客户端

!!爬网的步骤

1 使用HTTPresquest工具模拟HTTP请求, 接收到返回的文本

用于请求的包request 安装 pip install request

2 对接收的文本进行筛选 获取想要的内容

用于筛选文本的包 bs4 lxml

pip install bs4 \pip install lxml

导入模拟http请求的包

from bs4 import BeautifulSoup

导入用于分析的包

import lxml
import re

BASE_URL = “http://www.ilync.cn/org/6818_d_0_0_-1_-1_0_1”
COURSE_DETAIL_URL = “http://www.ilync.cn/kecheng/detail_{}?f=org_coursecenter”

import requests

encoding=utf-8

def get_content(url:str):
# 定义 一个headers
header = {
# ‘referer’: ‘http: // sc.weather.com.cn / mianyang / index.shtml’,
# ‘User – Agent’:
# ‘’’
# Mozilla / 5.0(Windows NT 6.1;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 78.0.3904.108Safari / 537.36
# ‘’’
}
response = requests.get(url,params=header)
response_test = response.content.decode(“utf-8”)
return response_test

def get_pages(one_page_number: int):
“”“获取当前的”””
# 拼接一个URL
url = BASE_URL + “1”
# 获取结果
content = get_content(url)
# 实例化BS4对象
soup = BeautifulSoup(content, ‘lxml’)
# 第一次筛选
total_courses = int(soup.find(‘input’, id=‘countCourseNum’).attrs[‘value’])
# 计算页码
return int(total_courses / one_page_number) + 1

def get_pages_url(pages: int):
“”“拼接页码”””
# 定义一个list
page_list = []
# 循环
for page in range(1, pages + 1):
# 附加到list
page_list.append(BASE_URL + str(page))

# 返回
return page_list

def get_course_detail_info(url: str):
“”“爬取课程的明细:多少次观看,多少次评价,多少次收藏”””
# 获取明细也的内容
content = get_content(url)

# 实例化BS4对象
soup = BeautifulSoup(content, 'lxml')
# 筛选
first_filter = soup.find_all('span', class_="fcz")  # 返回list
# 定义一个temp_dict
temp_dict = {}
# 添加学习次数
temp_dict['study_times'] = int(re.findall(r"\d{1,}", str(first_filter[0]))[0])
# 添加评价次数
temp_dict['evaluate_times'] = int(re.findall(r"\d{1,}", str(first_filter[1]))[0])
# 添加收藏次数
temp_dict['collect_times'] = int(re.findall(r"\d{1,}", str(first_filter[2]))[0])
# 返回
return temp_dict

def get_all_course(urls: list):
“”“爬取所有的页”””
# 定义一个list存储结果
all_course = []
# 开始遍历
for url in urls:
# 根据url爬取这一页的课程
all_course.extend(get_course_info(url))
# 返回
return all_course

def get_course_info(url: str):
“”“根据文本信息抓取内容 “””
content = get_content(url)
# 实例化BS4对象
soup = BeautifulSoup(content, ‘lxml’)
# 第一次筛选
first_filter = soup.find_all(‘div’, class_=‘course-list-wrap’)[0]
# 第二次筛选
second_filter = first_filter.find_all(‘div’, class_=“grid-cell”)
# 定义一个集合 — [{},{},{},{},…]
courses_infos = []
# 使用循环来遍历第二次筛选的内容
for one in second_filter:
# # 临时定义个字典
temp_dict = {}
# # 获取课程的Id
id_str = one.find(‘a’, class_=“course-pic”).attrs[‘href’]
id = id_str[id_str.find(“_”) + 1:id_str.find(“?”)]
# # 添加id到字典
temp_dict[‘id’] = id
# # 把课程的URL添加到集合中
temp_dict[‘url’] = COURSE_DETAIL_URL.format(id)
# # 调用明细课程的爬虫并合并到现在字典
temp_dict.update(get_course_info(temp_dict[‘url’]))

    # 获取图片地址
    img = one.find('img').attrs['src']
    temp_dict['img'] = img
    # # 下载图片
    # save_image(img)
    # 获取课程名称
    title = one.find('img').attrs['title']
    temp_dict['title'] = title
    # 获取课程类别
    type = one.find('div', class_="course-courseware-cate").text
    temp_dict['type'] = type
    # 价格--
    price_list = one.find('div', class_="course-price").text.replace("\t", "").split('\n')
    # 遍历并判断
    for one_price in price_list:
        if "免费" in one_price or "." in one_price:
            temp_dict['price'] = one_price
    # 课时
    time = one.find('div', class_='course-courseware-num').find('span').text
    temp_dict['time'] = time
    # 附加到集合
    courses_infos.append(temp_dict)

# 返回
return courses_infos

if name == ‘main’:
url=“http://www.ilync.cn/org/6818_d_0_0_-1_-1_0_1”
# # # 获取有多少页
pages = get_pages(24)
# # 拼接每一页的URLS
urls = get_pages_url(pages)
# # # 获取整个页面的课程
results = get_course_info(url)
# # 存储
# save_to_mysql(results)
# 打印具体课程
for one in results:
print(one)
print(“=” * 50)
# 打印多少课程
print(len(results))

本文地址:https://blog.csdn.net/weixin_44766722/article/details/111105771