我有两个xpath,其中只有一个正确地从下面的url中提取作业标题。任何想法为什么xpath1(我发现使用'检查元素/复制XPath'功能的Chrome)不起作用,而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返回['人类学家''考古学家',...]
答案 0 :(得分:3)
似乎没有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']