I am gonna to use Google App Script to fetch the programme list from the website of radio station. How can I select the specified elements in the webpage by specifying the id of the element? Therefore, I can get the programs in the webpage.
相关问题
- How can I force all files in a folder to be owned
- Scraping all mobiles of Flipkart.com
- Google Apps Script: testing doPost() with cURL
- Selenium in Java is not finding element when using
- How to filter out nodes with rvest?
相关文章
- Is there a google API to read cached content? [clo
- DOM penalty of using html attributes
- How to create an SVG Matrix without an SVG element
- Parsing complex HTML tables
- How to allow access for importrange function via a
- Google app script trigger not working
- Append new attribute with setAttribute()?
- Set Date/Time to 00:00:00
I'm going to assume that you are referring to using UrlFetchApp's
fetch()
method. In which case, the answer is no, in the context of what you are thinking of.If you look at the return type for
fetch()
in the documentation it returns HTTPResponse. There are a few methods for that, but most of them involve getting the returned data as a string. The good news is, you could still use any (well, most) of the traditional JS String methods documented here - so you could usesearch()
,match()
, etc. Depending on your project you could use those to find the data you are looking for in the response.Someone has made an example here where the following custom functions are available for cut & paste use:
Then you can do something like this
Edit, Dec 2013: Google has deprecated the old
Xml
service, replacing it withXmlService
. The script in this answer has been updated to use the new service. The new service requires standard-compliant XML & HTML, while the old one was forgiving of such problems as missing close-tags.Have a look at the Tutorial: Parsing an XML Document. (As of Dec 2013, this tutorial is still on line, although the Xml service is deprecated.) Starting with that foundation, you can take advantage of the XML parsing in Script Services to navigate the page. Here's a small script operating on your example:
To get the real page, start with this:
If you look at the documentation for
getElements
you'll see that there is support for retrieving specific tags, for example "div". That finds direct children of a specific element, it doesn't explore the entire XML document. You should be able to write a function that traverses the document examining theid
of eachdiv
element until it finds your programme list.Edit - I couldn't help myself...
Here's a utility function that will do just that.
Applying this to your example, we get this:
Note: See this answer for a practical example of the use of these utilities.