gpt4 book ai didi

java - 使用 Java 和 JSoup 从 HTML 文件中抓取数据 - Android Studio

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:07 25 4
gpt4 key购买 nike

我正在 Android Studio 中从本地 HTML 页面抓取数据,但没有显示正确的信息,而是显示了空白屏幕。该信息不是从 HTML 文件中获取的(它没有显示在日志中)。下面是主要代码段。有谁知道怎么解决吗?

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
StringBuilder buf=new StringBuilder();
InputStreamReader inputStream = new InputStreamReader(getAssets().open("filename.html"));
BufferedReader bufferedReader = new BufferedReader(inputStream);
String str;
while ((str=bufferedReader.readLine()) != null) {
buf.append(str);
}
Document doc = Jsoup.parse(buf.toString());
StringBuilder sb = new StringBuilder();

Elements tableElements = doc.select("td[colspan='1']");
TextView textView = (TextView)findViewById(R.id.text_view);
for (Element td : tableElements) {
sb.append(td.text() + "\n");
System.out.println(td.text());
}
textView.setText(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}

下面是我正在尝试抓取的 HTML。我想抓取“9:00”和“9:15”这样的时间。还有“Sound”、“P.Mann”、“P2016”、“Ship”、“P2015”、“K.Lion”和“22-29、32-36”。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>timetable.html</title>
<meta http-equiv='content-disposition', content='attachment;filename=timetable.html'>
<meta http-equiv="Content-Type" content="application/octet-stream" />

<style>body {background-color:white;} body,td { font-family: arial; } </style>
</head>
<data>
<body>
<table cellspacing='0' border='0' width='100%' >
<col align='left' />
<col align='center' />
<col align='right' />
</data>
<tr>
<td/>
<td/>
<td/>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table cellspacing='0' border='0' width='100%' >
<col align='left' />
<col align='center' />
<col align='right' />
<tr>
<td/>
<td/>
<td/>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table cellspacing='0' border='0' width='100%' >
<col align='left' />
<col align='center' />
<col align='right' />
<tr>
<td/>
<td/>
<td/>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table cellspacing='0' border='0' width='100%' >
<col align='left' />
<col align='center' />
<col align='right' />
<tr>

<td>
<table border='0' width='100%'>
<tr>
<td width='40%' align='left' valign='middle'>
<font face='arial' size='3'>
<b>The Year<font size='1'>
</td>
<td width='20%' align='center' valign='middle'>
<font face='arial' size='1'>ICOM</td>
<td width='40%' align='right' valign='middle'>
<font face='arial' size='2'>
<b>Weeks selected for output: 26 (22 Feb 2016-28 Feb 2016)</td>
</td>
<td/>
<td/>
</tr>
</table>
</td>
</tr>
</table>
<table cellspacing='0' border='1'>
<tr>
<td/>

<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>9:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>9:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>9:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>9:45</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>10:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>10:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>10:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>10:45</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>11:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>11:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>11:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>11:45</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>12:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>12:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>12:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>12:45</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>13:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>13:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>13:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>13:45</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>14:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>14:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>14:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>14:45</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>15:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>15:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>15:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>15:45</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>16:00</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>16:15</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>16:30</font>
</td>
<td bgcolor='#C0C0C0' colspan='1'>
<font color='#FFFFFF'>16:45</font>
</td>
</tr>
<tr >
<td style="border-bottom:3px solid #000000;" rowspan='1' bgcolor='#C0C0C0'>
<font color='#FFFFFF'>Monday</font>
</td>
<td style="border-bottom:3px solid #000000;" colspan='12' rowspan='1' >

<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<tr>
<td align='left'>
<font color='#FF0000'>Sound</font>
</td>
</tr>
</table>
<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<col align='right' />
<tr>
<td align='left'>
<font color='#000000'>P2016</font>
</td>
<td align='right'>
<font color='#008000'>P.Man</font>
</td>
</tr>
</table>
<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<tr>
<td align='left'>
<font color='#000080'>22-29, 32-36</font>
</td>
</tr>
</table>

</td>
<td style="border-bottom:3px solid #000000;" >&nbsp;</td>
<td style="border-bottom:3px solid #000000;" >&nbsp;</td>
<td style="border-bottom:3px solid #000000;" >&nbsp;</td>
<td style="border-bottom:3px solid #000000;" >&nbsp;</td>
<td style="border-bottom:3px solid #000000;" colspan='4' rowspan='1' >

<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<tr>
<td align='left'>
<font color='#FF0000'>Ship</font>
</td>
</tr>
</table>
<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<col align='right' />
<tr>
<td align='left'>
<font color='#000000'>P2015</font>
</td>
<td align='right'>
<font color='#008000'>K.Loin</font>
</td>
</tr>
</table>
<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<tr>
<td align='left'>
<font color='#000080'>22-29, 32-36</font>
</td>
</tr>
</table>

</td>
<td style="border-bottom:3px solid #000000;" colspan='12' rowspan='1' >

<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<tr>
<td align='left'>
<font color='#FF0000'>Mobility</font>
</td>
</tr>
</table>
<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<col align='right' />
<tr>
<td align='left'>
<font color='#000000'>P2222</font>
</td>
<td align='right'>
<font color='#008000'>D.Met</font>
</td>
</tr>
</table>
<table cellspacing='0' border='0' width='100%'>
<col align='left' />
<tr>
<td align='left'>
<font color='#000080'>22-29, 32-36</font>
</td>
</tr>
</table>

</td>
</tr>

最佳答案

编辑您的选择器以包含“Sound”、“P.Mann”、“P2016”、“Ship”、“P2015”、“K.Lion”和“22-29、32-36”:

Elements tableElements = doc.select("td[colspan='1'],td[align='left'],td[align='right']");

关于java - 使用 Java 和 JSoup 从 HTML 文件中抓取数据 - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36405665/

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