需求:
使用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)