gpt4 book ai didi

python - 如何使用 python 从 HTML 页面中提取特定数据?

转载 作者:行者123 更新时间:2023-12-02 00:47:31 25 4
gpt4 key购买 nike

我是 python 的新手,我有一个 HTML 文本文件,我想用 python 2.7 抓取它。

以下代码只是一家公司信息的示例。在完整的 html 文本文件中,所有其他公司的代码结构也是相同的,并且位于彼此之下(如果后面的信息有帮助)。

基本上,我想按时间顺序提取某些信息(如公司名称、位置、电话号码和网站),以便将数据分配给正确的组织,如下所示:

Liberty Associates LLC | New York    | +1 973-344-8300 | www.liberty.edu
Company B | Los Angeles | +1 213-802-1770 | perchla.com

如果我不够简洁,我很抱歉,但任何关于如何启动脚本及其外观的建议都会非常有帮助!

代码:

<body><div class="tab_content-wrapper noPrint"><div class="tab_content_card">
<div class="card-header">
<strong title="" d.="" kon.="" nl="">"Liberty Associates LLC"</strong>
<span class="tel" title="Phone contacts">Phone contacts</span>

</div>
<div class="card-content">


<table>
<tbody>
<tr>
<td colspan="4">

<label class="downdrill-sbi" title="Industry: Immigration">Industry: Immigration</label>
</td>
</tr>
<tr>
<td width="20">&nbsp;</td>
<td width="245">&nbsp;</td>
<td width="50">&nbsp;</td>
<td width="80">&nbsp;</td>
</tr>
<tr>
<td colspan="2">
59 Wall St</td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2">NJ 07105&nbsp;&nbsp;

<label class="downdrill-sbi" title="New York">New York</label>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><td>Phone:</td><td>+1 973-344-8300</td><td>Firm Nr:</td><td>KL4568TL</td></tr>
<tr><td>Fax:</td><td>+1 973-344-8300</td><td colspan="2"></td></tr>
<tr>
<td colspan="2"> <a href="http://www.liberty.edu/" target="_blank">www.liberty.edu</a> </td>
<td>Active:</td>
<td>Yes</td>
</tr>
</tbody>
</table>
</div>


</div></div></body>

它在网页上的样子:

enter image description here

编辑:

所以在 ajputnam 的帮助下,我现在得到了这个:

from lxml import html    

str = open('test_html.txt', 'r').read()
tree = html.fromstring(str)

name = tree.xpath("/html/body/div/div/div[1]/strong/text()")
place = tree.xpath("/html/body/div/div/div[2]/table/tbody/tr[4]/td[1]/label/text()")
phone = tree.xpath("/html/body/div/div/div[2]/table/tbody/tr[6]/td[2]/text()")
url = tree.xpath("/html/body/div/div/div[2]/table/tbody/tr[8]/td[1]/a/text()")

print(name, place, phone, url)

打印:

(['"Liberty Associates LLC"'], ['New York'], ['+1 973-344-8300'], ['www.liberty.edu'])

但是,当我在整个 html 文件(包含多个公司数据)上尝试此代码时,我发现所有匹配变量都紧随其后。我怎样才能正确使用 [0] 来获得这样结构的数据?:

Liberty Associates LLC | New York    | +1 973-344-8300 | www.liberty.edu
Company B | Los Angeles | +1 213-802-1770 | perchla.com

最佳答案

首先,您需要从页面获取 HTML。您可以使用类似请求的库来执行此操作。

from lxml import html
import requests

page = requests.get('url')
tree = html.fromstring(page.content)

然后您可以使用选择器访问“树”中的内容。

prices = tree.xpath('//span[@class="item-price"]/text()')

或者您可以正常解析字符串。

参见:HTML scrapping

从文件读取

from lxml import html

# read html as string from file
str = open('file.html', 'r').read()
tree = html.fromstring(str)

company = tree.xpath('//div[@class="card-header"]/strong/text()')
print company

关于python - 如何使用 python 从 HTML 页面中提取特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42683764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com