Show div from another website

Deadly 提交于 2019-12-17 12:37:32

问题


Is there a way to use an iframe or some other method of showing a named div from another website?

I want to pull in some data from a government website into a google map and when they click the point I want the information from one of the divs on that page to display.


回答1:


I take assumption that you are sure of div's ID in that other website.

If yes. use Jquery Ajax to pull the site's content into a hidden iframe in your site. then fetch the content of the div-in-question into some variable and then you can use it for your purpose (parse html table data or do whatever)

Discard the iframe's content so that you don't have unnecessary items in your page's DOM.




回答2:


Using JQuery you should be able to exactly that with the load-function. Here is a small example to get a container with id "container" on a page called Test.html:

$('#contentDiv').load('/Test.hmtl #container');

You can visit the JQuery documentation here for more info




回答3:


  1. Ajax Call
  2. In-House Service to Scrape the HTML from the page
  3. Select the div with xpath / SGML parser
  4. Return to ajax call-handler
  5. Replace the content of your div

However There are other problems, i.e. scraping someone's site for their content without their permission is BAD.

They may or may not care, but still you should seek permission, or one day you could find your webserver blacklisted from their site. Or worse... Especially a government site.

You should probably go about figuring out how to properly obtain the data you need (perhaps there's an api somewhere) and then render your own version.




回答4:


You would have to make use of either JSONP or a middle-agent to retrieve the data (i.e. a PHP script using the CURL library).

JSONP functionality is included in numerous Javascript libraries such as MooTools and jQuery. That is the method I would use.

MooTools: http://mootools.net/docs/more/Request/Request.JSONP

jQuery: http://docs.jquery.com/Release:jQuery_1.2/Ajax

Once you have retrieved the body of the page the DIV resides on, you could use REGEX to extract the information from the specific DIV element.



来源:https://stackoverflow.com/questions/2828109/show-div-from-another-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!