【发布时间】:2010-12-21 08:20:33
【问题描述】:
在回答previous question 时,有几个人建议我将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”的链接,但不返回律师姓名。因此,出于某种原因,BeautifulSoup 看不到这些链接(例如“/cabel”)。我不明白为什么。
【问题讨论】:
-
你试过用双引号代替单引号吗:
<a href="/cabel">...</a>. -
据我所知,BeautifulSoup 没有正确解析页面,soup.contents 在文档开头的标签
<a href="https://www.whitecasealumni.com/jsp/Front/login.jsp" target="_blank">之后没有给出任何内容。
标签: python xpath beautifulsoup