bs4的BeautifulSoup模块使用,

本文介绍了如何使用Python的BeautifulSoup模块进行网页抓取和解析,包括地址获取方法和直接查找属性的技巧,帮助读者快速入门网络爬虫。
摘要由CSDN通过智能技术生成

引用

from bs4 import BeautifulSoup    
html=requests.get('https://www.cnblogs.com/cate/python/')
soup=BeautifulSoup(html.text,'lxml')

地址获取方法

items=soup.select('div[class="post_item_body"]')
for item in items:
	import requests
from bs4 import BeautifulSoup

headers={
    'User-Agent':'https://www.cnblogs.com/cate/python/',
    'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'cache-control':'max-age=0'
}
html=requests.get('https://www.cnblogs.com/cate/python/')
soup=BeautifulSoup(html.text,'lxml')
items=soup.select('div[class="post_item_body"]')
for item in items:
    title=item.select('h3 a[class="titlelnk"]')[0].get_text()
    href = item.select('h3 a[class="titlelnk"]')[0]['href']
    author=item.select('div a[class="lightblue"]')[0].get_text()
    author_home= item.select('div a[class="lightblue"]')[0]['href']
    infos=item.select('p[class="post_item_summary"]')[0].get_text().strip('\n').strip(' ')
    datas=item.select('div[class="post_item_foot"]')[0].get_text()
    datas=datas.split(' ')
# ['\n随风奔跑的少年', '\r\n', '', '', '', '发布于', '2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值