简体   繁体   English

HTML / JavaScript解析动态表

[英]HTML/JavaScript parsing dynamic table

This is probably a stupid question, but I want to use JavaScript to get the HTML from this website . 这可能是一个愚蠢的问题,但是我想使用JavaScript从该网站获取HTML。 I want data from the center of the page, but it looks to be a dynamic table (you can click tabs and the URL doesn't change, nor does the table reload); 我想要页面中心的数据,但它看起来像是一个动态表(您可以单击选项卡,URL不变,表也不会重新加载); this might be AJAX? 这可能是AJAX吗? Anyway, I wrote a query that should have accessed data in the table, but it came back undefined. 无论如何,我写了一个查询,该查询应该已经访问了表中的数据,但是它返回时未定义。 I tested the query on a regular table in JSFiddle, and it worked, but the same query didn't pull any information from the table. 我在JSFiddle的常规表上测试了该查询,并且该查询有效,但同一查询并未从该表中提取任何信息。 I'm getting the HTML using XMLHttpRequest ; 我正在使用XMLHttpRequest获取HTML; will this not work for such a table? 这样的桌子不行吗?

This is not a good way to go about it. 这不是解决问题的好方法。 The first thing you are going to run into is the Same Origin Polacy . 您要遇到的第一件事是Same Origin Polacy Since you are building a Chrome extension this should not be an issue, but flashscore could change their HTML in the future which would messup your website. 由于您正在构建Chrome扩展程序,因此这不是问题,但是flashscore将来可能会更改其HTML,这会使您的网站混乱。

Rather than having your UI depend on their UI I would use an API. 与其让UI依赖于他们的UI,不如使用API​​。 If you are just trying to get sports scores in general (not from flashscore) there are plenty of APIs out there. 如果您只是想获得总体运动成绩(而不是通过flashscore),则可以使用许多API。 I would look specifically for a Javascript one because those are the easiest to use with a Chrome Extension. 我会专门寻找一种Javascript,因为它们最容易在Chrome扩展程序中使用。 With this info you can build whatever kind interface you want. 使用此信息,您可以构建所需的任何类型的界面。 I google a few API with Javascript wrappers. 我用Java包装器在Google上搜索了一些API。

Here are a couple. 这是一对。

I am not sure how good these are, but you can give them a try. 我不确定这些有多好,但是您可以尝试一下。

I believe a headless browser (no viewport) like PhantomJS could do the trick, or you could write your own Chrome Extension Content Script , which injects JS when your window.location matches a pattern. 我相信像PhantomJS这样的无头浏览器( 无视口)可以达到目的,或者您可以编写自己的Chrome扩展内容脚本 ,当window.location与模式匹配时注入JS。 You could either manually or automatically open the page (commandline open http://http://flashscore.com/ ), which would trigger your content script to run and even close itself 您可以手动或自动打开页面(命令行open http://http://flashscore.com/ ),这将触发您的内容脚本运行甚至关闭自身

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM