Encoding Problem when filling out a text_field with Watir in Ruby

烈酒焚心 提交于 2019-12-24 02:56:06

问题


I'm using Watir to fill out a text_field with the html-code that I have scraped with another program before.

The language of the website-content that I'm transfering is German, so there are some special characters involved, that don't exist in the English alphabet.

Those characters are displayed properly in the html-file, but when transfered into the text_field of the Joomla installation (I'm transfering a website to Joomla with this program), the special characters are not displayed properly.

As the result of a users great help, I have been able to solve a previous problem and am now transfering the content with the following method:

browser.text_field(:id => "text").value=(open('my-site.html') { |f| f.read })

The result was, that the special characters were shown as follows:

über => ³ber 
vergißt => vergi▀t 
wählen => wõhlen 
geförderter => gef÷rderter 

The user guessed that it had something to do with the codepage that I'm on and encoding issues. Running DOS: chcp resulted in the output of 850.

His attempt to solve the problem was the following:

require 'iconv'
browser.text_field(:id => "text").value=(
  Iconv.iconv('CP850', 'ISO-8859-1', open('my-site.html') { |f| f.read })
)

Unfortunately, this didn't solve the problem and the special characters are now shown for example as: \x81ber = über vergi\xE1t = vergißt and new lines are shown as \n

I scraped the pages with the Mechanize, using the following code:

auszug=page.search ('/html/body/table/tr/td/table/tr[2]/td/table/tr/td[4]')
outputFile<<auszug

I hope you can somehow help me, as I'm just a volunteer working here with a bit of programming experience. If I don't get this program running by next week (this encoding thing is the only thing that's really stopping me), then I'll have to manually transfer a hundred pages using copy+paste :/

Thanks for taking the time and all the effort you're putting into this! :-)

Sebastian


回答1:


Did you try converting to UTF-8?

browser.test_field(:id => "text").value=(Iconv.conv(‘utf-8’, 'CP850', open('my-site.html') {|f| f.read})


来源:https://stackoverflow.com/questions/5974692/encoding-problem-when-filling-out-a-text-field-with-watir-in-ruby

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