我有两个 xpath,其中只有一个正确地从下面的 url 中提取职位。知道为什么 xpath1(我使用 Chrome 的“检查元素/复制 XPath”功能发现它)不起作用,而 xpath2 起作用吗?
import requests
from lxml import html
url = 'http://www.mynextmove.org/find/browse?c=54'
xpath1 = '//*[@id="content"]/table[1]/tbody/tr/td[1]/a/text()'
xpath2 = '//a[contains(@href, "profile")]/text()'
page = requests.get(url)
tree = html.fromstring(page.text)
jobs = tree.xpath(xpath2)
print 'jobs:', jobs
xpath1 返回 [],即空列表。
xpath2 返回 ['人类学家'、'考古学家'、...]
最佳答案
没有 tbody
它看起来像将其更改为:
`xpath1 = '//*[@id="content"]/table[1]/tr/td[1]/a/text()'`
并尝试一下。
当我这样做时,这就是我得到的:
In [31]: tree.xpath(xpath1)
Out[31]:
['Anthropologists',
'Archeologists',
'Architects',
'Architectural Drafters',
'Biochemists & Biophysicists',
'Civil Drafters',
'Civil Engineers',
'Environmental Engineering Technicians',
'Environmental Engineers',
'Geodetic Surveyors',
'Lawyers',
'Legal Secretaries',
'Mapping Technicians',
'Marine Architects',
'Marine Engineers',
'Paralegals & Legal Assistants',
'Survey Researchers',
'Surveying Technicians',
'Surveyors',
'Tax Preparers',
'Transportation Engineers',
'Veterinarians',
'Veterinary Assistants & Laboratory Animal Caretakers',
'Veterinary Technologists & Technicians',
'Water/Wastewater Engineers']
关于python - lxml xpath 找不到 anchor 文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30381143/