博客
关于我
Python每日一练(11)-爬取在线课程
阅读量:116 次
发布时间:2019-02-26

本文共 4776 字,大约阅读时间需要 15 分钟。

???????Python????

1. ?????Excel??

???????????????????????????Python????????????????????????????????????????????????????????Python?????????Python????????????????Excel????

????????????????????????????????????????????????????????????????????Python??????????????????????Python???????????Excel????????????????

2. ??requests??????

??????????????requests?????HTTP?????????????????????????????????

  • ????????????????????????????user-agent??????????
  • ??????????????????????????????????????????
  • ??JSON??????????JSON?????????json()???????

3. ??xlsxwriter????

????????????????Excel????????xlsxwriter?????????????????????xlsxwriter??????

  • ?????????????xlsxwriter???
pip install xlsxwriter
  • ???????????xlsxwriter???

  • ??Excel?????Workbook???Excel???????????Worksheet?

  • ???????write()???????Excel???????????Excel???????0???

4. ??????????

??????????????????

  • ?????????????????????????????????????????????
  • ??????????????????????????????????????????????????????????????????
  • ??????????????????????????????????????????????

5. ???????????

??????????????????????????????????????????????????????????????????????

6. ?????MySQL

????????Excel???????????????MySQL????????????????

  • ??????MySQL???????????????????????
  • ???????SQL???????????????????????????

7. ????

??????????????????requests?pymysql???????????Python???????????MySQL?????

import requestsimport timefrom multiprocessing import Poolfrom pymysql import *# ??????????MySQLdef get_json(index):    url = "https://study.163.com/p/search/studycourse.json"    payload = {        "pageSize": 50,        "pageIndex": index,        "relativeOffset": 0,        "searchTimeType": -1,        "orderType": 5,        "priceType": -1,        "activityId": 0,        "qualityType": 0,        "keyword": "python"    }    headers = {        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36",        "accept": "application/json",        "content-type": "application/json",        "origin": "https://study.163.com"    }    response = requests.post(url, json=payload, headers=headers)    if response.status_code == 200:        content_json = response.json()        if content_json and content_json["message"] == "ok":            return content_json    return Nonedef get_content(content_json):    if "result" in content_json:        return content_json["result"]["list"]    return []def check_course_exit(course_id):    sql = f"select course_id from course where course_id = {course_id}"    cs1.execute(sql)    course = cs1.fetchone()    if course:        return True    else:        return Falsedef save_to_course(course_data):    sql_course = """insert into course                   values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)                   """    cs1.executemany(sql_course, course_data)def save_mysql(content):    course_data = []    for item in content:        if not check_course_exit(item['courseId']):            course_value = (                item['courseId'],                item['productId'],                item['productType'],                item['productName'],                item['provider'],                item['score'],                item['scoreLevel'],                item['learnerCount'],                item['lessonCount'],                item['lectorName'],                item['originalPrice'],                item['discountPrice'],                item['discountRate'],                item['imgUrl'],                item['bigImgUrl'],                item['description']            )            course_data.append(course_value)    save_to_course(course_data)def main(index):    content_json = get_json(index)    content = get_content(content_json)    save_mysql(content)if __name__ == '__main__':    conn = connect(host="localhost", port=3306, database="wyy_spider", user="root", password="mysql", charset="utf8")    cs1 = conn.cursor()    print("*******************????*******************")    start = time.time()    total_page_count = get_json(1)['result']["query"]["totlePageCount"]    pool = Pool()    index_list = [i for i in range(total_page_count)]    pool.map(main, index_list)    pool.close()    pool.join()    conn.commit()    cs1.close()    conn.close()    print("????")    end = time.time()    print(f"???????{end - start}?")    print("*******************????*******************")

8. ????

  • get_json???????HTTP?????JSON??????????????????????????????
  • get_content????JSON????????????
  • check_course_exit????????????????????
  • save_to_course?????????????????
  • save_mysql???????????????????save_to_course?????
  • main?????????????????????????????????

9. ????

???????????????????????MySQL????????????????course????????????

10. ??

???????????????????????????????Python????????Excel?MySQL???????????????????????????????????????????????????????????????????

转载地址:http://blvk.baihongyu.com/

你可能感兴趣的文章
PHP 设置调试工具XDebug PHPStorm IDE
查看>>
php 身份证号检测
查看>>
PHP 输入输出流合集
查看>>
PHP 过滤器(Filter)
查看>>
php 运算符and or && || 的详解
查看>>
php 返回html字符串长度限制,记一次js中和php中的字符串长度计算截取的终极问题和完美...
查看>>
php 阿里云oss 上传回调
查看>>
PHP 面向对象 final类与final方法
查看>>
php+JQ+EasyUI自动加载数据
查看>>
php+sql server根据自增序号id区间查询第几条到第几条的数据
查看>>
php--------获取当前时间、时间戳
查看>>
Redis使用场景举例
查看>>
php--正则表达式
查看>>
php--防止sql注入的方法
查看>>
PHP-CGI Windows平台远程代码执行漏洞复现(CVE-2024-4577)
查看>>
php-cgi耗尽报502错误
查看>>
php-cgi(fpm-cgi) 进程 CPU 100% 与 file_get_content...
查看>>
PHP-DI/Invoker 开源项目使用教程
查看>>
php-fpm与Nginx运行常见错误说明
查看>>
php-fpm比php成为apache模块好在哪
查看>>