Python爬虫 获取CSDN文章总数 获取任意页面的标签和属性(包括获取a标签的href属性)

import requests
from bs4 import BeautifulSoup
# 构造请求头
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
r=requests.get('https://blog.csdn.net/qq_27694835',headers=headers)
soup=BeautifulSoup(r.text,'html.parser')
listTotal=soup.findAll(attrs={'id':'container-header-blog'}) 
print(listTotal) # [<li class="active margin" data-num="75" id="container-header-blog"><span>博客(75)</span></li>]
for target in listTotal:
    try:
        value = target.get('data-num')
    except:
        value = ''
    if value:
        print(value) #75

简写:

import requests
from bs4 import BeautifulSoup
# 构造请求头
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
r=requests.get('https://blog.csdn.net/qq_27694835',headers=headers)
soup=BeautifulSoup(r.text,'html.parser')
listTotal=soup.find(attrs={'id':'container-header-blog'}) # # [<li class="active margin" data-type="75" id="container-header-blog"><span>博客(75)</span></li>]

print(listTotal)
print(listTotal.get('data-num')) # 75



正则表达式形式

import requests,re
from bs4 import BeautifulSoup
# 构造请求头
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
r=requests.get('https://blog.csdn.net/qq_27694835',headers=headers)
soup=BeautifulSoup(r.text,'html.parser')
listTotal=soup.find(attrs={'id':'container-header-blog'})
#正则表达式形式 \d 匹配数字 \d+  尽量匹配多位数字
print(re.findall(r'\d+',str(listTotal))[0]) #78


参考:python爬虫获取任意页面的标签和属性(包括获取a标签的href属性)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值