【发布时间】:2015-02-14 16:04:05
【问题描述】:
我有一些嵌入了数据的代码。这是一个示例:
<div class='clear' ></div>
</div> <!-- findResultListing -->
<div class='findResultListing ' id='result_listing_7_0' onclick='examMapManagerHandle.clickMarker(7,0);'>
<a href='javascript:examMapManagerHandle.clickMarker(7,0);'>
<img class='balloon' src='/system/themes/asp/img/gmarkerH.png' border='0' />
</a>
<div class='findResultInfo'>
<div class="nextStep">
<a href="/system/modules/shibboleth/secure_find/shib_gateway.php?url=%2Fexams%2Fschedule.php%3Fnav%3Dexams%2Cstucourses%2Cexams%2Csched_exam%26amp%3Badd_locid%3D1672">
<img height="16" border="0" align="left" width="16" src="/system/themes/asp/img/schedule.png"/>Schedule Exam
</a>
</div>
<a href='javascript:examMapManagerHandle.clickMarker(7,0);' >
SJSU Testing
<img class='userType' border='0' src="/system/themes/asp/img/org.png" alt='Testing Site' title='Testing Site'/>
</a>
<br />
One Washington Square<br />
Industrial Studies Building 228<br />
San Jose, CA 95112<br />
Phone: (408) 924-5980<br />
Email: <span id="_smarty_mailto_span_2096382943_1423929156_8"> </span>
<noscript>To see email address, enable javascript</noscript>
<script type="text/javascript">var mailto=document.getElementById("_smarty_mailto_span_2096382943_1423929156_8");
mailto.innerHTML='<a href="mailto:testing-office@sjsu.edu" >testing-office@sjsu.edu</a>';</script><br />
Fee for two hour exam:
$40.00
<a class="helpBtn" onmouseover="asp_toolTip(this,' <strong>Fee Details:<\/strong> We charge $20 for the first hour and $10 for each half hour after... <br /> <strong>Miscellaneous Fees:<\/strong> Test emailed in pdf/Word Doc., we will charge an administrative fee of $15 for 10 or more test pages <br /> <strong>Parking Fee Details:<\/strong> Its $8.00 to park in the 10th St. garage on the corner of 9th & E. San Fernando Sts.', 'findResultsToolTip', 'fit_west', 'map_results_pane');"></a>
<br />
</div><!-- findResultInfo -->
我只想从上面的代码中提取以下内容:
SJSU考试考场
华盛顿广场一号
工业研究大楼 228
加利福尼亚州圣何塞 95112
电话:(408) 924-5980
邮箱:testing-office@sjsu.edu
两小时考试费用:40.00 美元
我可以通过哪些方式自动从代码中提取这些数据?
【问题讨论】:
-
建议的解决方案对您有用吗?
-
@aberna 我不确定如何“使用”Xpath。我应该在 Chrome 中使用它(它可以通过“检查元素”获得)......还是有一些我应该使用的应用程序?我熟悉 Xpath 作为一种在网页中定位内容的技术......并且已经使用过它从 Excel 和 Google 表格中 --- 我应该使用其中之一来执行此操作吗?
-
Xpath 用于导航元素。所有主要语言都提供不同的接口来使用 xpath。在此处查看en.wikipedia.org/wiki/XPath 以了解您喜欢使用的编程语言的实现。
-
在 SO 上查看各种方法 (stackoverflow.com/questions/tagged/xpath)。由于您想自动化提取过程,我建议您开始使用 Lxml、BeautifulSoup、Scrapy 等作为 Python 的爬虫
-
您可以使用 Xpath 和 Regex 获得结果。很大程度上取决于您将用于实现自动化的工具
标签: web-scraping screen-scraping text-parsing text-extraction data-extraction