我有一個包含大約 400 條客戶評論的大型 HTML 檔案。有沒有一種工具可以用來廢棄檔案并從中獲取特定資料并將它們放入 CSV 檔案中?目標是將這些評論從舊網站轉移到同一家公司的新網站。
包含每條評論的 HTML 如下所示(該檔案有 400 個這樣的塊):
<section class="reviews-16465185">
<div class="internal-review">
<div class="">
<div class="left">
<div class="reviewer"><span>Joe K.</span></div>
<div class="internal-rating">
<div class="top" style="width: 100%">
<i class="fa fa-star"></i><i class="fa fa-star"></i
><i class="fa fa-star"></i><i class="fa fa-star"></i
><i class="fa fa-star"></i>
</div>
<div class="bottom">
<i class="fa fa-star-o"></i><i class="fa fa-star-o"></i
><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i
><i class="fa fa-star-o"></i>
</div>
</div>
<div class="review-rating">
<meta content="1" />
<meta content="5.0" />
<meta content="5" />
</div>
<div class="review-date">
<meta content="2022-01-05" />Submitted 01/05/22
</div>
</div>
<div class="right">
<div class="type-full">
<span
>Review goes here Review Goes Here Review Goes Here</span
>
</div>
</div>
</div>
</div>
</section>
我需要獲取的資料是審閱者的姓名、評級、日期和審閱。
我更喜歡 js、node js、php 或 python 中的工具。
uj5u.com熱心網友回復:
使用python(雖然它也可以用js和php完成)并利用xpath,您可以嘗試以下操作:
import lxml.html as lh
reviews = """your html above"""
doc = lh.fromstring(reviews)
sections = doc.xpath('//section')
for section in sections:
reviewer = section.xpath('.//div[@]/span/text()')[0]
date = section.xpath('.//div[@]/meta/@content')[0]
review = section.xpath('.//div[@]/span/text()')[0]
rating = section.xpath('.//div[@]//meta/@content')[1]
print(f"{date}, {reviewer}, {review}, {rating}")
輸出應該是
2022-01-05, Joe K., Review goes here Review Goes Here Review Goes Here, 5.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417672.html
標籤:
上一篇:.HTML文本的遍歷決議
