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