标签:coding 爬取 blog war utf-8 for ble 内容 content
#coding:utf-8
from bs4 import BeautifulSoup
import requests
import urllib3
#禁用安全请求警告
urllib3.disable_warnings()
#===============以下是爬取博客园首页摘要====================
r=requests.get("https://www.cnblogs.com/xiaohuhu/")
#请求首页后获取整个html界面
blog=r.content
#print(blog)
#用html.parser解析html
soup=BeautifulSoup(blog,"html.parser")
#获取所有标题日期,class属性为dayTitle,返回列表
times=soup.find_all(class_=‘dayTitle‘)
#获取所有摘要标题,class属性为postTitle
titles=soup.find_all(class_="postTitle")
#获取所有摘要内容,class属性为postCon
descs = soup.find_all(class_="postCon")
for i,j,k in zip(times,titles,descs):
print(i.a.string) #循环打印a标签的文本
print(j.a.string)
print(k.div.contents[0]) #标签div的.contents属性可以将标签的子节点以列表的方式输出,取第一个
#===============以下是爬取糗事百科段子====================
r = requests.get("https://www.qiushibaike.com/", verify=False)
qiubai = r.content
soup = BeautifulSoup(qiubai, "html.parser")
duanzi = soup.find_all(class_="content")
for i in duanzi:
# tag的 .contents 属性可以将tag的子节点以列表的方式输出
duan = i.span.contents[0] # 取第一个
print(duan)
标签:coding 爬取 blog war utf-8 for ble 内容 content
原文地址:https://www.cnblogs.com/xiaohuhu/p/9386092.html