简体   繁体   中英

Getting images and text from webpage with javascript and yql

trying to learn how to parse pages with yql execute. I want to return the text and images separately. I also want to change the width of all the images. Tried to write javascript for this but it isn't working. Can anyone point me in the right direction?

  <execute><![CDATA[ var q = y.query('select * from html where url=@url and xpath=@xpath',{url:url,xpath:xpath}); var myresults = q.results; var images = myresults.getElementsByTagName('img'); images.setAttribute('width','150'); text = myresults.getElementById("p").textContent; response.object = <text>{text}</text> <images>{images}</images>; ]]> </execute> 

Actually you can do something very similar to what you want by just using th html table that is built into YQL. Here an example where I am selecting img and p elements from this article .

I use two different XPath expressions within one Xpath:

//div[@id='left_col']//img
//div[@id='left_col']//p

The full query:

SELECT * FROM html WHERE url="http://www.computerworld.com/s/article/9209140/Sentiment_analysis_comes_of_age" AND xpath="//div[@id='left_col']//img|//div[@id='left_col']//p"

Try it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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