如何将此XPath表达式转换为BeautifulSoup?

如何将此XPath表达式转换为BeautifulSoup?,第1张

如何将此XPath表达式转换为BeautifulSoup?

我知道BeautifulSoup是规范的HTML解析模块,但是有时您只想从某些HTML中抓取一些子字符串,而pyparsing有一些有用的方法可以做到这一点。使用此代码:

from pyparsing import makeHTMLTags, withAttribute, SkipToimport urllib# get the HTML from your URLurl = "http://www.whitecase.com/Attorneys/List.aspx?LastName=&FirstName="page = urllib.urlopen(url)html = page.read()page.close()# define opening and closing tag expressions for <td> and <a> tags# (makeHTMLTags also comprehends tag variations, including attributes, # upper/lower case, etc.)tdStart,tdEnd = makeHTMLTags("td")aStart,aEnd = makeHTMLTags("a")# only interested in tdStarts if they have "class=altRow" attributetdStart.setParseAction(withAttribute(("class","altRow")))# compose total matching pattern (add trailing tdStart to filter out # extraneous <td> matches)patt = tdStart + aStart("a") + SkipTo(aEnd)("text") + aEnd + tdEnd + tdStart# scan input HTML source for matching refs, and print out the text and # href valuesfor ref,s,e in patt.scanString(html):    print ref.text, ref.a.href

我从您的页面中提取了914条引用,从Abel到Zupikova。

Abel, Christian /cabelAcevedo, Linda Jeannine /jacevedoAcuña, Jennifer /jacunaAdeyemi, Ike /igbadegesinAdler, Avraham /aadler...Zhu, Jie /jzhuZídek, Aleš /azidekZiółek, Agnieszka /aziolekZitter, Adam /azitterZupikova, Jana /jzupikova


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5664468.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存