Retrieve contents of URL as string

前端 未结 7 781
温柔的废话
温柔的废话 2020-12-25 12:57

For tedious reasons to do with Hpricot, I need to write a function that is passed a URL, and returns the whole contents of the page as a single string.

I\'m close.

7条回答
  •  别那么骄傲
    2020-12-25 13:42

    To make code a little clearer, the OpenURI open method will return the value returned by the block, so you can assign open's return value to your variable. For example:

    xml_text = open(url) { |io| io.read }
    

提交回复
热议问题