【python爬虫】BeautifulSoup库的选择器select()方法

一般使用BeautififulSoup解析得到的Soup文档可以使用find_all()find()select() 方法定位所需要的元素。find_all()是获得list列表、find() 是获得map一条数据。select() 是根据选择器可以获得多条也可以获得单条数据。一般最常用的是find_all()find() 两个参数。

select()方法的使用
  1. 从页面中自定义获得选择器:
    F12中选择了目标element之后,右键—Copy—Copy selector 如图:
    这里写图片描述
  2. nth-child 在Python中运行会报错,需要改为 nth-of-type:
    如果所复制的选择器中包含nth-child,则需要改为nth-of-type,否则会报错。
  3. demo:
import requests
from bs4 import BeautifulSoup
url = 'http://www.cnplugins.com/'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36'}
res = requests.get(url,headers = headers) #get方法中加入请求头
#查看下当前requests请求url抓去的数据编码,这里获取的是ISO-8859-1
print (requests.get(url).encoding)
#翻阅下要爬去的网站的编码是什么,这里看了下是utf-8,编码不一样会乱码,将requests获取的数据编码改为和目标网站相同,改为utf-8
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser') #对返回的结果进行解析
# print (soup.select('body > section > div.wrapbox > div:nth-child(1) > div > ul > li:nth-child(6)'))
# nth-child 在python中运行会报错,需改为 nth-of-type
# print (soup.select('body > section > div.wrapbox > div:nth-of-type(1) > div > ul > li:nth-of-type(6)'))
textlist = soup.select('body > section > div.wrapbox > div > div > ul > li > div.iimg-box-meta > a')
for t in textlist:
    print (t) #获取单条html信息
    print (t.get_text()) #获取中间文字信息
  • 12
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值