ruby - nokogiri: how to wrap html tags around given xpath elements? -


I have an xpath to capture every text node that is not surrounded by any html tag. Instead, they are & lt; Br> Separated by means of . I'll call them & lt; Span & gt; I want to wrap the tag.

  Nokogiri :: HTML (open ("http://vancouver.en.craigslist.ca/ Van / swp / 1426164969.html")) .xpath ("// br / low- Sibling :: text () | br / pre-speech: text () "). To_a  

will return those text nodes.

The complete revised code below:

  Doctor = nocogiri :: HTML (open ("http: //vancouver.en.cde.gr. Craigslist.ca/van/swp/1426164969. Html ")). Except ("// br / low-sibling :: text () | // br / preceding-sibling :: text ()"). Hope to see a full HTML source code with the wrapped text ("& lt    ),  

But I got the following:

  Date: 2009-10-17, 4:36 AM Reply to PDT: This is a fantastic open plan 1000 square feet loft in a former Canadian post building From the hallway, you are surprised at entering the loft where you arrive ... a wonderful, bright and completely The built apartment which maintains its industrial experience, the restoration of the interior was planned and designed by a famous Vancouver architect, the loft is above a police station, so you must be guaranteed peace and plenty of time at any time of day or night. Neighborhood is safe and vibrant with many restaurants and shopping. There is a Starbucks on the street and there are plenty of coffee shops in the area. The ancient street with a hidden treasure is one block away, as well as the beautiful mile long boardwalk.The SkyTrain Station is a minute away (literally two buildings away) 15 minutes from commercial drive, 20 minutes for downtown Vancouver and Olympic venues . Features of the apartment: - Fully furnished - 14 feet terrace - Hardwood floor - Gas fireplace - Elevator - Large terrace balcony - Full kitchen: Fully crystal, with China and dishes - dishwasher - High end juice maker, Blender etc. Equipment including - WiFi (Wireless Internet) - Bathtub - Lincoln & amp; Amp; Towels provided - Hair dryer - LCD flat-screen TV with DVD player - Comprehensive DVD library - Music library: iPod connection - Wii console, game-book and magazine library with guitar hero - Non-smoking Looking also from a place in the hot (California, Hawaii, Mexico, South America, Central America) or Europe (UK, Italy, France). Emails for other dates and staging photos  

Your document variable is not specified in the entire document - You should use

  Doctor = Nocogiri :: HTML (open ("http://vancouver.en.craigslist.ca/van/swp/1426164969.html")) Doc.xpath ("// br / lower-brother :: text () | // br / predecessor-cybling :: text ()"). Therap (" ") tells the doctor  

Unfortunately this does not solve the problem because the nocagri is the first text Along with all the spans, all BR is in place:

   & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Span & gt; & Lt; / Span & gt; & Lt; Period & gt; & Lt; / Span & gt; & Lt; Period & gt; & Lt; / Span & gt; & Lt; Period & gt; & Lt; / Span & gt;  

But you can do this

  doc = Nokogiri :: HTML (open ("http://vancouver.en.craigslist.ca/van /swp/1426164969.html ")) doc.search (" // br / lower-sibling :: text () | // br / preceding speech :: text () "). Every do Node | Node.replace (nokogiri.make ("& lt; span & gt; # {node.to_html} ")) Finally puts the dos  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -