Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在Python中从html中刮取无序列表?_Python 3.x_Xpath - Fatal编程技术网

Python 3.x 如何在Python中从html中刮取无序列表?

Python 3.x 如何在Python中从html中刮取无序列表?,python-3.x,xpath,Python 3.x,Xpath,我试图用Python从thesaurus.com中提取同义词,他们使用无序列表列出同义词 from lxml import html import requests term = (input("Enter in a term to find the synonyms of: ")) page = requests.get('http://www.thesaurus.com/browse/' + term.lower(),allow_redirects=True) if page.status_

我试图用Python从thesaurus.com中提取同义词,他们使用无序列表列出同义词

from lxml import html
import requests
term = (input("Enter in a term to find the synonyms of: "))
page = requests.get('http://www.thesaurus.com/browse/' + term.lower(),allow_redirects=True)
if page.status_code == 200:
    tree = html.fromstring(page.content)
    synonyms = tree.xpath('//div[@class="relevancy-list"]/text()')
    print(synonyms)
else:
    print("No synonyms found!")
我的代码只输出空格,而不是同义词。如何清除实际的同义词而不是空格。

/text()
仅打印当前标记下的文本。因此,当前代码不会打印同义词,因为它位于
div
标记内的另一个标记下

您应该使用
//text()
打印当前标记下的所有文本。但这将打印所有文本,包括不必要的文本

对于您的用例,由于同义词位于
标记中,因此可以使用以下XPath:

//div[@class="relevancy-list"]//span[@class="text"]/text()
它选择在具有类“关联列表”的div中具有类“text”的范围内找到的所有文本

对于输入术语
set
,使用该XPath的输出为:

['firm', 'bent', 'stated', 'specified', 'rooted', 'established', 'confirmed', 'pat', 'immovable', 'obstinate', 'ironclad', 'predetermined', 'intent', 'entrenched', 'appointed', 'regular', 'prescribed', 'determined', 'scheduled', 'fixed', 'settled', 'certain', 'customary', 'decisive', 'definite', 'inveterate', 'pigheaded', 'resolute', 'rigid', 'steadfast', 'stubborn', 'unflappable', 'usual', 'concluded', 'agreed', 'resolved', 'stipulated', 'arranged', 'prearranged', 'dead set on', 'hanging tough', 'locked in', 'set in stone', 'solid as a rock', 'stiff-necked', 'well-set', 'immovable', 'entrenched', 'located', 'solid', 'situate', 'stiff', 'placed', 'stable', 'fixed', 'settled', 'situated', 'rigid', 'strict', 'stubborn', 'unyielding', 'hidebound', 'positioned', 'sited', 'jelled', 'hard and fast', 'deportment', 'comportment', 'fit', 'presence', 'mien', 'hang', 'carriage', 'air', 'turn', 'attitude', 'address', 'demeanor', 'position', 'inclination', 'port', 'posture', 'setting', 'scene', 'scenery', 'flats', 'stage set', u'mise en sc\xe8ne', 'series', 'array', 'lot', 'collection', 'batch', 'crowd', 'cluster', 'gang', 'bunch', 'crew', 'circle', 'body', 'coterie', 'faction', 'company', 'bundle', 'outfit', 'band', 'clique', 'mob', 'kit', 'class', 'clan', 'compendium', 'clutch', 'camp', 'sect', 'push', 'organization', 'clump', 'assemblage', 'pack', 'gaggle', 'rat pack', 'locate', 'head', 'prepare', 'fix', 'introduce', 'turn', 'settle', 'lay', 'install', 'put', 'apply', 'post', 'establish', 'wedge', 'point', 'lock', 'affix', 'direct', 'rest', 'seat', 'station', 'plop', 'spread', 'lodge', 'situate', 'plant', 'park', 'bestow', 'train', 'stick', 'plank', 'arrange', 'insert', 'level', 'plunk', 'mount', 'aim', 'cast', 'deposit', 'ensconce', 'fasten', 'embed', 'anchor', 'make fast', 'make ready', 'zero in', 'appoint', 'name', 'schedule', 'make', 'impose', 'stipulate', 'settle', 'determine', 'establish', 'fix', 'specify', 'designate', 'decree', 'resolve', 'rate', 'conclude', 'price', 'prescribe', 'direct', 'value', 'ordain', 'allocate', 'instruct', 'allot', 'dictate', 'estimate', 'regulate', 'assign', 'arrange', 'lay down', 'agree upon', 'fix price', 'fix', 'stiffen', 'thicken', 'condense', 'jelly', 'clot', 'congeal', 'solidify', 'cake', 'coagulate', 'jell', 'gelatinize', 'crystallize', 'jellify', 'gel', 'become firm', 'gelate', 'drop', 'subside', 'sink', 'vanish', 'dip', 'disappear', 'descend', 'go down', 'initiate', 'begin', 'raise', 'abet', 'provoke', 'instigate', 'commence', 'foment', 'whip up', 'put in motion', 'set on', 'stir up'] 【坚定的】、【弯曲的】、【规定的】、【规定的】、【扎根的】、【确立的】、【确认的】、【拍拍的】、【固定的】、【固执的】、【铁腕的】、【预定的】、【意图的】、【根深蒂固的】、【指定的】、【规定的】、【确定的】、【确定的】、【习惯的】、【决定性的】、【确定的】、【根深蒂固的】、【固执的】、【坚定的】、adfast、顽固、镇定、惯常、总结、同意、解决、规定、安排、预先安排、死心塌地、死心塌地、坚如磐石、僵硬的脖子、稳固的、固定的、不动的、稳固的、稳固的、稳固的、稳固的、稳固的、稳固的、稳固的、稳固的、稳固的僵硬的、严格的、顽固的、不屈的、呆板的、定位的、定位的、僵硬的、坚定的、举止的、举止的、健康的、在场的、风度的、悬挂的、马车的、空中的、转身的、态度的、地址的、举止的、位置的、倾斜的、港口的、姿势的、背景的、场景的、风景的、平坦的、舞台布景的、u'mise en sc\xe8n“e”、“系列”、“阵列”、“批次”、“集合”、“批次”、“人群”、“集群”、“帮派”、“帮派”、“团队”、“团队”、“小圈子”、“派系”、“公司”、“捆绑”、“装备”、“乐队”、“集团”、“暴徒”、“套件”、“班级”、“氏族”、“纲要”、“离合器”、“营地”、“门派”、“推送”、“组织”、“集群”、“集合”、“包装”、“嘎嘎”、“老鼠包”、“定位”、“hea”“准备”、“修复”、“引入”、“旋转”、“安置”、“铺设”、“安装”、“放置”、“应用”、“张贴”、“建立”、“楔子”、“尖头”、“锁”、“粘贴”、“直接”、“休息”、“座位”、“车站”、“扑通”、“散开”、“住宿”、“安置”、“种植”、“公园”、“授予”、“火车”、“棍子”、“木板”、“排列”、“插入”、“水平”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“插入”、“sconce、紧固、嵌入、锚定、快速制造、准备就绪、零入、指定、名称、计划、制造、强制、规定、结算、确定、确定、修复、指定、指定、法令、决议、费率、结论、价格、规定、直接、价值、命令、分配、指示、分配、口述、估计“调整”、“分配”、“安排”、“放置”、“商定”、“定价”、“固定”、“硬化”、“加厚”、“浓缩”、“果冻”、“凝块”、“凝固”、“凝固”、“蛋糕”、“凝固”、“果冻”、“糊化”、“结晶”、“凝胶”、“凝胶”、“变硬”、“糊化”、“滴落”、“沉降”、“下沉”、“消失”、“浸渍”、“消失”、“下降”、“下降”、“启动”,‘开始’、‘提高’、‘教唆’、‘挑衅’、‘煽动’、‘开始’、‘煽动’、‘鞭策’、‘启动’、‘启动’、‘煽动’] 请注意,您将获得该词所有词义的同义词

您可能希望手动循环查看
//div[@class=“relevancy list”]
的结果,并为找到的每个
div
提取
//span[@class=“text”]/text()
,以获得每个意义的同义词

import requests
from bs4 import BeautifulSoup

term = input("Enter in a term to find the synonyms of: ")
page = requests.get('http://www.thesaurus.com/browse/' + term.lower(), allow_redirects=True)

if page.status_code == 200:
    soup = BeautifulSoup(page.content, 'html.parser')
    get_syn_tag = soup.find('div', {'class': 'relevancy-list'})
    list_items = get_syn_tag.findAll('li')
    synonyms = []  # to fetch synonym anytime used list to append all synonyms
    for i in list_items:
        synonym = i.find('span', {'class':'text'}).text
        print(synonym) # prints single synonym on each iteration
        synonyms.append(synonym) # appends synonym to list
else:
    print("No synonyms found!")
查找所有
li
标记将更加精确,但在这种情况下,下面的行也将起作用:

synonym_list = [i.text for i in get_syn_tag.findAll('span', {'class':'text'})] # this will create a list of all available synonyms if there is no other `span` tag with same class `text` in the specified `div`