在回答上一个问题时,有几个人建议我将BeautifulSoup用于我的项目。我一直在努力处理他们的文档,但我无法解析它。有人可以指出我应该能够将此表达式转换为 BeautifulSoup 表达式的部分吗?
hxs.select('//td[@class="altRow"][2]/a/@href').re('/.a\w+')
上面的表达式来自Scrapy。我正在尝试应用正则表达式re('\.a\w+')
以td class altRow
从那里获取链接。
我也将不胜感激任何其他教程或文档的指针。我找不到任何东西。
谢谢你的帮助。
编辑: 我正在查看此页面:
>>> soup.head.title
<title>White & Case LLP - Lawyers</title>
>>> soup.find(href=re.compile("/cabel"))
>>> soup.find(href=re.compile("/diversity"))
<a href="/diversity/committee">Committee</a>
但是,如果您查看页面源代码"/cabel"
:
<td class="altRow" valign="middle" width="34%">
<a href='/cabel'>Abel, Christian</a>
出于某种原因,BeautifulSoup 看不到搜索结果,但 XPath 可以看到它们,因为hxs.select('//td[@class="altRow"][2]/a/@href').re('/.a\w+')
捕获了“/cabel”
编辑: cobbal:它仍然无法正常工作。但是当我搜索这个时:
>>>soup.findAll(href=re.compile(r'/.a\w+'))
[<link href="/FCWSite/Include/styles/main.css" rel="stylesheet" type="text/css" />, <link rel="shortcut icon" type="image/ico" href="/FCWSite/Include/main_favicon.ico" />, <a href="/careers/northamerica">North America</a>, <a href="/careers/middleeastafrica">Middle East Africa</a>, <a href="/careers/europe">Europe</a>, <a href="/careers/latinamerica">Latin America</a>, <a href="/careers/asia">Asia</a>, <a href="/diversity/manager">Diversity Director</a>]
>>>
它返回所有带有第二个字符“a”的链接,但不返回律师姓名。因此,出于某种原因,BeautifulSoup 看不到这些链接(例如“/cabel”)。我不明白为什么。