爬虫项目实战1——爬取天善课程

代码参考自课程
项目内容:爬取天善智能学院课程的名称、讲师、价格
项目工具:re,requests
项目代码:

# @Time : 2020/4/1 20:32 
# @Author : w
# @File : 爬取天善课程.py 
# @Software: PyCharm
import re
import requests
#利用for循环实现相似网页的连续爬取
for i in range(0,100):
    urls="https://edu.hellobi.com/course/"+str(i+1)
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
    }
    response=requests.get(urls,headers=headers)
#正则表达式来匹配 (.*?)括号里是需要的,返回的部分
    title_pa='<li class="active">(.*?)</li>'
    teacher_pa='class="lec-name">(.*?)<'
    price_pa = 'm-price.*?</sub>(.*?)</span>'
#用findall来查找内容,if-else对内容做一个判断
    title=re.compile(title_pa, re.S).findall(response.text)
    if(len(title)>0):
        print(title)
    else:
        continue
    teacher=re.compile(teacher_pa, re.S).findall(response.text)
    if(len(teacher)>0):
        print(teacher)
    else:
       print('lost')
    price=re.compile(price_pa, re.S).findall(response.text)
    if(len(price)>0):
        print(price)
    else:
        print("free")
    print("------------------------------")
#代码截至2020.4.1测试无误

学习点:
利用for循环来更换页面
利用字符串相加urls="https://edu.hellobi.com/course/"+str(i+1)来改变url
带请求头

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值