如何处理Ruby中的'Unclosed quotation mark after the character string'?

How to handle 'Unclosed quotation mark after the character string' in Ruby?

我在尝试从网页抓取 html 并将其保存到我的活动记录对象时遇到错误。我收到的消息是 Unclosed quotation mark after the character string。 Ruby 中是否有函数或方法可以在将记录保存到数据库之前检查并纠正发生这种情况的实例? TinyTDS 的完整错误如下...

ActiveRecord::StatementInvalid: TinyTds::Error: Unclosed quotation mark after the character string '

更新

抱歉,我没有意识到我没有添加足够的信息。我会很乐意添加任何请求。让我从日志开始,并正确地介绍关于 "escaping my data" 的部分——它是来自外部源的 HTML 引发的错误。所以至此,我想也许我回到了我最初的问题,即如何找到未闭合的引号所在的位置,然后更正它?

错误日志

  SQL (27.8ms)  BEGIN TRANSACTION
  SQL (2.8ms)  EXEC sp_executesql N'UPDATE [school_player_scrapes] SET [biography_text] = @0, [headshot_url] = @1, [updated_at] = @2 WHERE [school_player_scrapes].[id] = @3; SELECT @@ROWCOUNT AS AffectedRows', N'@0 nvarchar(max), @1 nvarchar(4000), @2 datetime, @3 int', @0 = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
                <p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended Butte CC in 2015.</span></p>
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">High School:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.�</span></p>
            </td></body></html>
', @1 = N'http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg', @2 = '12-15-2016 08:24:50.657', @3 = 7444  [["biography_text", "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><td class=\"sm PlayerBioArticleContent\" colspan=\"2\">\n\t\t\t\t<p><strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">Butte College:\xA0</span></strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">Attended Butte CC in 2015.</span></p>\r\n<p><strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">High School:\xA0</span></strong><span style=\"font-family: arial, helvetica, sans-serif; font-size: small;\">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.\xA0</span></p>\n\t\t\t</td></body></html>\n"], ["headshot_url", "http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg"], ["updated_at", Thu, 15 Dec 2016 08:24:50 EST -05:00], ["id", 7444]]
TinyTds::Error: Unclosed quotation mark after the character string '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
                <p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:'.: EXEC sp_executesql N'UPDATE [school_player_scrapes] SET [biography_text] = @0, [headshot_url] = @1, [updated_at] = @2 WHERE [school_player_scrapes].[id] = @3; SELECT @@ROWCOUNT AS AffectedRows', N'@0 nvarchar(max), @1 nvarchar(4000), @2 datetime, @3 int', @0 = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
                <p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended Butte CC in 2015.</span></p>
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">High School:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.�</span></p>
            </td></body></html>
', @1 = N'http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg', @2 = '12-15-2016 08:24:50.657', @3 = 7444
  SQL (0.8ms)  IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
Performed ScrapeSchoolRosterJob from Sidekiq(high_priority) in 2787.93ms
ActiveRecord::StatementInvalid: TinyTds::Error: Unclosed quotation mark after the character string '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
                <p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:'.: EXEC sp_executesql N'UPDATE [school_player_scrapes] SET [biography_text] = @0, [headshot_url] = @1, [updated_at] = @2 WHERE [school_player_scrapes].[id] = @3; SELECT @@ROWCOUNT AS AffectedRows', N'@0 nvarchar(max), @1 nvarchar(4000), @2 datetime, @3 int', @0 = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><td class="sm PlayerBioArticleContent" colspan="2">
                <p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Butte College:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended Butte CC in 2015.</span></p>
<p><strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">High School:�</span></strong><span style="font-family: arial, helvetica, sans-serif; font-size: small;">Attended North Little Rock High Scchool . . . Ranked a two-star recruit by Scout.com and 247 Sports . . . Recruited by schools such as Auburn, Georgia Tech and Minnesota among others.�</span></p>
            </td></body></html>
', @1 = N'http://image.cdnllnwnl.xosnetwork.com/pics33/200/IQ/IQQZSKTMHZNYBCD.20160807033143.jpg', @2 = '12-15-2016 08:24:50.657', @3 = 7444
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:336:in `each'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:336:in `handle_to_names_and_values_dblib'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:325:in `handle_to_names_and_values'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:300:in `_raw_select'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:295:in `block in raw_select'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:295:in `raw_select'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:244:in `sp_executesql'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:19:in `exec_query'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `exec_update'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-sqlserver-adapter-4.2.3/lib/active_record/connection_adapters/sqlserver/database_statements.rb:39:in `exec_update'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:114:in `update'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/relation.rb:88:in `_update_record'
... 57 levels...
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:337:in `call'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:337:in `block in simple'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_perform_callbacks'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/execution.rb:31:in `perform_now'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/activejob-4.2.0/lib/active_job/execution.rb:16:in `perform_now'
    from (irb):2
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/deploy/bane/shared/bundle/ruby/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'

我能够更正此问题的方法是通过执行以下代码将我试图保存的字符串从 ISO-8859-1 转换为 UTF-8my_string.encode!('UTF-8')。这删除了导致和问题的无效字符。