栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将此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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/662513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号