python 爬虫获取网页指定信息内容

# -*- coding: UTF-8 -*-
# @Time: 2021/6/3 10:19
# @Author: wuling
# @Version: V1.0
# @File: mytest.py
# @desc: 练习爬虫

from bs4 import BeautifulSoup  # 网页解析,获取数据
import re  # 正则表达式,进行文字匹配`
import urllib.request, urllib.error  # 制定URL,获取网页数据
import xlwt  # 进行excel操作
#import sqlite3  # 进行SQLite数据库操作

findLink = re.compile(r'<a href="/python/python.*?" target="_top" title="Python .*?".*?')
# findLink = re.compile(r'<a href="(.*?)">')


url = "https://www.runoob.com/python/python-tutorial.html"
head = {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Mobile Safari/537.36"} # 模拟浏览器 用户代理
request = urllib.request.Request(url, headers=head)

response = urllib.request.urlopen(request)
html = response.read().decode('utf-8')  # 网页源码

soup1 = BeautifulSoup(html, 'html.parser')
# soup2=BeautifulSoup(html, '1xml')
# find = soup1.find('div')

find = soup1.find_all("a")
data = []
for i in find:
    result=re.findall(findLink, str(i))
    if result:
        res=result[0].split('=')[-1]
        data.append(res)
# find = soup1.find(href="/python/python")
for i in data:
    print("***************这是第%s课,课程叫%s****************"%(data.index(i),i))

# print("find's return type is ", type(find))               # 输出返回值类型
# print("find's content is", find)                          # 输出find获取的值
# print("find's Tag Name is ", find.name)                   # 输出标签的名字
# print("find's Attribute(class) is ", find['class'])       # 输出标签的class属性值

爬出来的结果数据见下图:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值