回答上一个问题时,有几个人建议我在我的项目中使用BeautifulSoup。我一直在苦苦挣扎他们的文档,但我无法解析它。有人能指导我应该去哪个部分将此表达式转换为BeautifulSoup表达式吗?
hxs.select('//td[@class="altRow"][2]/a/@href').re('/.a\w+')
上面的表达式来自Scrapy。我正在尝试将正则表达式
re('\.a\w+')
应用于td class altRow
以从中获取链接。我也希望获得有关任何其他教程或文档的指针。我找不到任何东西。
谢谢你的帮助。
编辑: 我正在查看这个page:
>>> 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"的链接,但不包括律师姓名。因此,某些链接(如“/cabel”)在BeautifulSoup中不可见。我不明白为什么。
<a href="/cabel">...</a>
。 - jfs<a href="https://www.whitecasealumni.com/jsp/Front/login.jsp" target="_blank">
标签后面什么也没有。 - cobbal