聚焦爬虫案例3:扇贝单词

需求:
使用xpath将单词以及解释提取出来

import requests
from lxml import etree

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}

response = requests.get(url='https://www.shanbay.com/wordlist/110521/232414/?page=1',headers=headers)
# print(response.text)

将字符串转换成html元素对象

html = etree.HTML(response.text)

使用xpath提取数据
先获取tr标签

tr_list = html.xpath('//table[@class="table table-bordered table-striped"]/tbody/tr')
print(tr_list)  # [<Element tr at 0x24c2c3bd8c8>, <Element tr at 0x24c2c3bd888>, <Element tr at 0x24c2c3bd848>, <Element tr at 0x24c2c3bd808>, <Element tr at 0x24c2c3bd708>, <Element tr at 0x24c2c3e4088>, <Element tr at 0x24c2c3e40c8>, <Element tr at 0x24c2c3e4708>, <Element tr at 0x24c2c3e4688>, <Element tr at 0x24c2c3cc888>, <Element tr at 0x24c2c3e4948>, <Element tr at 0x24c2c3e4988>, <Element tr at 0x24c2c3e4908>, <Element tr at 0x24c2c3e49c8>, <Element tr at 0x24c2c3e4a08>, <Element tr at 0x24c2c3e4a48>, <Element tr at 0x24c2c3e4a88>, <Element tr at 0x24c2c3e4ac8>, <Element tr at 0x24c2c3e4b08>, <Element tr at 0x24c2c3e4b48>]
for tr in tr_list:

获取单词

 word = tr.xpath('./td[@class="span2"]/strong/text()')[0]
    # print(word)

获取解释

  explain = tr.xpath('./td[@class="span10"]/text()')[0]
print(word,explain)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值