- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从网站抓取数据并使用网站数据生成 csv 文件。我在我的程序中使用 opencsv jar 以及 selenium jar 文件。 CSV 文件正在生成,但在每一行之后,都会生成一个空白行。我曾尝试消除相同的情况,但未能成功。这是我的代码:-
package automation;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import com.opencsv.CSVWriter;
import com.opencsv.exceptions.CsvException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import java.time.ZonedDateTime;
public class RecruitmeentTestApp {
public static void main(String[] args) throws IOException, CsvException, CsvRequiredFieldEmptyException {
//WebDriver driver = new FirefoxDriver();
//Fetching Remote Control driver for Chrome browser
WebDriver driver = new ChromeDriver();
driver.get("https://www.premierleague.com/tables/");
driver.manage().window().maximize();
//Setting the file name in milliseconds of execution as per requirement
long miliSec=ZonedDateTime.now().toInstant().toEpochMilli();
File file = new File(miliSec+ ".csv") ;
WebElement table=driver.findElement(By.xpath("//table"));
List<WebElement> rowsList = table.findElements(By.tagName("tr"));
//List<WebElement> headerList=rowsList.get(0).findElements(By.xpath(".//th"));
List<WebElement> columnsList = null;
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
CSVWriter csvWriter = new CSVWriter(writer,
CSVWriter.DEFAULT_SEPARATOR,
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
//As the table is static so creating headerRecord and writing it to csv file
String[] headerRecord = {"More","Postion", "Club", "Played", "Won","Drawn","Lost","GF","GA","GD","Points","Next"};
csvWriter.writeNext(headerRecord);
for (WebElement row : rowsList) {
System.out.println();
columnsList = row.findElements(By.tagName("td"));
String[]colText=new String[columnsList.size()];
int i=0;
for(WebElement column: columnsList) {
colText[i]=column.getText();
i++;
}
//writing the output to csv file
csvWriter.writeNext(colText);
}
//Closing the stream
csvWriter.close();
}
}
最佳答案
在 HTML 中 https://www.premierleague.com/tables/ , 每个可见的 tr 后面都有一个隐藏的折叠 tr。(其中有 colspan='13')
<tr class="tableDark" data-compseason="210" data-filtered-entry-size="20" data-filtered-table-row="1" data-filtered-table-row-name="Arsenal" data-filtered-table-row-opta="t3" data-filtered-table-row-abbr="1">
<td class="revealMore" style="display: table-cell;" tabindex="0" role="button">
<div class="icn chevron-down-g"></div>
</td>
<td class="pos" tabindex="0">
<span class="value">1</span>
</td>
<td class="team" scope="row">
<a href="/clubs/1/Arsenal/overview"><span class="badge-25 t3"></span> <span class="long">Arsenal</span><span class="short">ARS</span></a>
</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td class="hideSmall">0</td>
<td class="hideSmall">0</td>
<td>
0
</td>
<td class="points">0</td>
<td class="nextMatchCol hideMed">
<span tabindex="0" class="button-tooltip" id="Tooltip">
<span class="nextMatch"><span class="badge-20 t43"><span class="visuallyHidden">Manchester City</span></span></span>
<a href="/match/38308" class="tooltipContainer linkable tooltip-link tooltip-right" role="tooltip">
<span class="tooltip-content">
<div class="matchAbridged">
<span class="matchInfo">Saturday 11 August 2018</span>
<span class="teamName"><abbr title="Arsenal">ARS</abbr></span>
<span class="badge-20 t3"></span>
<time>15:00</time>
<span class="badge-20 t43"></span>
<span class="teamName"><abbr title="Manchester City">MCI</abbr></span>
<span class="icn arrow-right"></span>
</div>
</span>
</a>
</span>
</td>
</tr>
<tr class="expandable" data-filtered-table-row-expander="1">
<td colspan="13">
<a href="/clubs/1/Arsenal/overview" class="expandableTeam">
<span class="badge-50 t3"></span>
<span class="teamName">Arsenal</span>
</a>
<div class="expandableFixtures">
<div class="resultWidget">
<div class="label"><strong>Next Fixture</strong> - Saturday 11 August 2018</div>
<a href="/match/38308" class="matchAbridged pre">
<span class="teamName"><abbr title="Arsenal">ARS</abbr></span>
<span class="badge-20 t3"></span>
<time>15:00</time>
<span class="badge-20 t43"></span>
<span class="teamName"><abbr title="Manchester City">MCI</abbr></span>
<span class="icn arrow-right"></span>
</a>
</div>
<div class="btnContainer">
<a href="/clubs/1/Arsenal/overview" class="btn-highlight" role="btn">Visit <span class="visuallyHidden">Arsenal </span>Club Page<span class="icn arrow-right-w"></span></a>
</div>
</div>
<div class="teamPerformanceStandingsArea" style="display:none;">
<header>
<h3 class="subHeader left">Performance Chart</h3>
<a href="/stats/comparison" class="btn right">Compare against another team<span class="icn arrow-right"></span></a>
</header>
<div class="teamPerformanceStandingsContainer"></div>
</div>
</td>
</tr>
因此,请跳过添加具有属性 colspan='13' 的所有备用 tr,这会导致向 csv 文件添加空行。
关于java - 删除 CSV 中的空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51040920/
我正在使用 LinqToExcel 轻松地将 Excel 数据导入 SQL Server。 var fileName = ConfigurationManager.AppSetting
在我的 MVVM WPF 应用程序中,我使用的是数据绑定(bind)流文档。我使用了 here 描述的技术能够将我的数据绑定(bind)到流文档。 我的流文档绑定(bind)到我的 View 模型中的
好吧,这很难解释。 场景: 我有一个 DataGrid 定义如下: 它有两个 header ,我需要添加数据,我做了很多研究建议使用 Obs
如果用户单击 DataGridView 底部的空白行并将焦点从 DataGridView 移开,则现在单击的行将处于指示对该行进行更改的状态。 是否可以告诉 DataGridView 取消将此行标记为
我必须迭代行并将其添加到列表中,但我得到的总行数是 Excel 工作表中存在的总行数,即 1001 行,但我只有 3 行,包括标题,我必须停止使用行号的大小进行循环。 我的代码是 Sheet
我有一个 csv 文件,其中包含必须从中删除的信息(通过删除相应的列来完成),我得到的输出是我所期望的,除了每行之间还给我空白行。我怎样才能摆脱那些? (我目前没有pandas,所以如果有解决这个问题
我是一名优秀的程序员,十分优秀!