1

I am learning to use scrapinghub.com which runs in python 2.x I have written a script which uses Scrapy, I have crawled a string like below:

%3Ctable%20width%3D%22100%25%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23ff0000%3Bfont-size%3A20pt%3Btext-align%3Acenter%3Bfont-weight%3Abold%22%3E%0D%0A%09%E6%84%9B%E8%BF%AA%E9%81%94%20adidas%20Energy%20Boost%20%E8%B7%AF%E8%B7%91%20%E4%BD%8E%E7%AD%92%20%E9%81%8B%E5%8B%95%20%E4%BC%91%E9%96%92%20%E8%B7%91%E9%9E%8B%20%E8%B7%91%E6%AD%A5%20%E6%85%A2%E8%B7%91%20%E9%A6%AC%E6%8B%89%E6%9D%BE%20%E5%81%A5%E8%BA%AB%E6%88%BF%20%E6%B5%81%E8%A1%8C%20%E7%90%83%E9%9E%8B%20%E5%A5%B3%E8%A3%9D%20%E5%A5%B3%E6%AC%BE%20%E5%A5%B3%20%E5%A5%B3%E9%9E%8B%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23000000%3Bfont-size%3A14pt%3Btext-align%3Acenter%22%3E%0D%0A%09%EF%BC%8A%E9%9D%88%E6%B4%BB%E3%80%81%E8%BC%95%E9%87%8F%E3%80%81%E8%88%92%E9%81%A9%E5%85%BC%E5%85%B7%E7%9A%84%E9%81%B8%E6%93%87%3Cbr%20%2F%3E%EF%BC%8A%E7%B0%A1%E7%B4%84%E7%8F%BE%E4%BB%A3%E7%9A%84%E7%94%A2%E5%93%81%E8%A8%AD%E8%A8%88%2C%E5%B9%B4%E8%BC%95%E5%A4%9A%E6%A8%A3%E5%8C%96%E7%9A%84%E9%85%8D%E8%89%B2%E6%96%B9%E6%A1%88%2C%E6%9B%B4%E7%82%BA%E7%AC%A6%E5%90%88%E5%B9%B4%E8%BC%95%E6%B6%88%E8%B2%BB%E8%80%85%E7%9A%84%E5%AF%A9%E7%BE%8E%E5%81%8F%E5%A5%BD%3Cbr%20%2F%3E%EF%BC%8A%E7%B0%A1%E5%96%AE%E7%9A%84%E7%B7%9A%E6%A2%9D%E5%92%8C%E4%B9%BE%E6%B7%A8%E7%9A%84%E8%A8%AD%E8%A8%88%2C%E6%8F%90%E4%BE%9B%E4%BA%86%E7%8D%A8%E7%89%B9%E7%9A%84%E7%A9%BF%E6%90%AD%E7%B5%84%E5%90%88%3Cbr%20%2F%3E%EF%BC%8A%E9%80%8F%E6%B0%A3%E8%88%87%E4%BF%9D%E8%AD%B7%E6%80%A7%2C%E7%B5%90%E5%90%88%E4%BA%86ADIDAS%E7%9A%84%E5%89%B5%E6%96%B0%E7%A7%91%E6%8A%80%2C%E5%89%B5%E9%80%A0%E4%BA%86%E5%AE%8C%E7%BE%8E%E7%9A%84%E7%94%A2%E5%93%81%3Cbr%20%2F%3E%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cdiv%20align%3D%22center%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fs.yimg.com%2Fwb%2Fimages%2F2B558E585E39649599A9A266349EABD17A4ABC18%22%20%2F%3E%3C%2Fdiv%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Ftable%3E%3Ctable%20width%3D%22100%25%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23000000%3Bfont-size%3A12pt%3Btext-align%3Aleft%3Bfont-weight%3A100%22%3E%0D%0A%09%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cdiv%20align%3D%22center%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fs.yimg.com%2Fwb%2Fimages%2F0F1A6CBFE6F6631189D491A17A2A2E7C388F194E%22%20%2F%3E%3Cdiv%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Ftable%3E%3Ctable%20width%3D%22100%25%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23000000%3Bfont-size%3A12pt%3Btext-align%3Aleft%3Bfont-weight%3A100%22%3E%0D%0A%09%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cdiv%20align%3D%22center%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fs.yimg.com%2Fwb%2Fimages%2FA0C9B09CAC784E2CA81A572E8F9F2E5721812607%22%20%2F%3E%3Cdiv%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Ftable%3E

Which always gives me the following:

<table width="100%"> <tr><td><p style="color:#fa6b81;font-size:18pt;text-align:center;font-weight:bold">(女) æ迪é ADIDAD ENERGY CLOUD W éæ°£ç¶²å¸ ç¾æ­ é» èè·ç¶  ä¼éé æ¢è·é</p></td></tr> <tr><td><p style="color:#000000;font-size:12pt;text-align:center"><font color="BLUE">â»æ¬è³£å ´åççºYAHOOè³¼ç©ä¸­å¿å°ç¨ï¼å¶å®å¹³å°è¥ä½¿ç¨æ¬ç«ç¸éåç~ç屬侵æ¬!!</font><BR><BR></p></td></tr> <tr><td><div align="center"><img src="https://s.yimg.com/wb/images/739F6D54CD0AA4440D67A8BF0E569B0229AB1B37" /></div></td></tr> </table><table width="100%"> <tr><td><p style="color:#000000;font-size:12pt;text-align:left;font-weight:100"></p></td></tr> <tr><td><div align="center"><img src="https://s.yimg.com/wb/images/91D28279378AF5E3C26740855775ECAD3A7F4A6B" /><div></td></tr> <tr><td></td></tr> </table><table width="100%"> <tr><td><p style="color:#000000;font-size:12pt;text-align:left;font-weight:100"></p></td></tr> <tr><td><div align="center"><img src="https://s.yimg.com/wb/images/B2237D69C0886CCF330AFA459E3C03BB4454D01B" /><div></td></tr> <tr><td></td></tr> </table><table width="100%"> <tr><td><p style="color:#000000;font-size:12pt;text-align:left;font-weight:100"></p></td></tr> <tr><td><div align="center"><img src="https://s.yimg.com/wb/images/B60D486A89EDBAFBFE824F00309D069517654050" /><div></td></tr> <tr><td></td></tr> </table><table width="100%"> <tr><td><p style="color:#000000;font-size:12pt;text-align:left;font-weight:100"></p></td></tr> <tr><td><div align="center"><img src="https://s.yimg.com/wb/images/57EAC1C8B09A019AC734F50FB51DB87D0B319002" /><div></td></tr> <tr><td></td></tr> </table><table width="100%"> <tr><td><p style="color:#000000;font-size:12pt;text-align:left;font-weight:100"></p></td></tr> <tr><td><div align="center"><img src="https://s.yimg.com/wb/images/CEC5C31984853968755AE7465BCB251C82676B0B" /><div></td></tr> <tr><td></td></tr> </table><table width="100%"> <tr><td><p style="color:#000000;font-size:12pt;text-align:left;font-weight:100"></p></td></tr> <tr><td><div align="center"><img src="https://s.yimg.com/wb/images/B065DFBACAEC5ABED898492265DEB710EA052358" /><div></td></tr> <tr><td></td></tr> </table>

I always get the garbage text (女) æ迪é ADIDAD ENERGY CLOUD W é氣網å¸

The conversion code from url encoded text to unicode is like below

special_text = re.sub("<.*?>", "", special_text)
special_text = re.sub("<!--", "", special_text)
special_text = re.sub("-->", "", special_text)
special_text = re.sub("\n", "", special_text)
special_text = special_text.strip()
special_text = unquote(special_text)
special_text = re.sub("\n", "", special_text)
special_text = re.sub("\r", "", special_text)
special_text = re.sub("\t", "", special_text)
special_text = u' '.join((special_text, '')).encode('utf-8').strip()

I have tried a lot of different codes like

special_text = special_text.encode('utf-8')
special_text = special_text.decode('utf-8')

Which either gives me error or still the garbage text

Not sure what is the proper way to convert to unicode?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
forestclown
  • 1,582
  • 4
  • 25
  • 39
  • 2
    You appear to be printing valid UTF-8 data to a console or terminal that isn't configured for UTF-8. `unquote(special_text)` produces valid UTF-8 encoded HTML for me. – Martijn Pieters Feb 06 '17 at 12:50
  • You may want to [force convert to ASCII](http://stackoverflow.com/a/1207479/2689986)? – shad0w_wa1k3r Feb 06 '17 at 12:51
  • 2
    @AshishNitinPatil: that'll not be helpful, as the content is Chinese. – Martijn Pieters Feb 06 '17 at 12:54
  • 1
    Use a proper HTML parsing library, like BeautifulSoup. `from bs4 import BeautifulSoup; from urllib import unquote; result = BeautifulSoup(unquote(special_text)).get_text('\n', strip=True)` produces very clean Unicode output without having to do a lot of regular expression work. However, as long as you keep looking at UTF-8 bytes in a console that is configured for a different codec, you won't be able to assert wether or not something works. – Martijn Pieters Feb 06 '17 at 12:56
  • If I just use unquote (special_text) it gives me garbage text, that's why I was trying to do it to encode. It works for me if I just do unquote if I ran it on my local environment using Python 3 though – forestclown Feb 06 '17 at 13:02
  • I will try bs4 if I can successfully add bs4 dependencies to my scrapinghub project – forestclown Feb 06 '17 at 13:03
  • @forestclown: are you using Windows? What version of Python are you using, 3.5 or later? – Martijn Pieters Feb 06 '17 at 13:05
  • I think it would help if you shared your scrapy "script" that is giving you this string. Scrapinghub does not add anything special on top of Scrapy, on the data extraction part at least. And you can also run scrapy spiders with Python 3 in Scrapinghub's cloud. – paul trmbrth Feb 06 '17 at 16:24
  • I have posted the script – forestclown Feb 07 '17 at 02:43

2 Answers2

4

Your data is perfectly valid UTF-8, encoded into a URL (so URLEncoded). Your output indicates you are looking at a Mojibake, where your own software (console, terminal, text editor), is using a different codec to interpret the UTF-8 data. I suspect your setup is using CP-1254:

>>> print text.encode('utf8').decode('sloppy-cp1254')  # codec from the ftfy project
æ„›è¿ªé” adidas Energy Boost 路跑 ä½ç­’ é‹å‹• 休閒 è·‘é‹ è·‘æ­¥ 慢跑 é¦¬æ‹‰æ¾ å¥èº«æˆ¿ æµè¡Œ çƒé‹ å¥³è£ å¥³æ¬¾ 女 女é‹
*éˆæ´»ã€è¼•é‡ã€èˆ’é©å…¼å…·çš„é¸æ“‡
*簡約ç¾ä»£çš„產å“設計,年輕多樣化的é…色方案,更為符åˆå¹´è¼•æ¶ˆè²»è€…的審ç¾å好
*簡單的線æ¢å’Œä¹¾æ·¨çš„設計,æ供了ç¨ç‰¹çš„ç©¿æ­çµ„åˆ
*é€æ°£èˆ‡ä¿è­·æ€§,çµåˆäº†ADIDAS的創新科技,創造了完ç¾çš„產å“

If you don't know how to fix your terminal, I suggest you write the data to a file instead and use an editor you can tell what codec to use to read the data:

import io
with io.open('somefilename.txt', encoding='utf8') as f:
    f.write(unicode_value)

I also strongly recommend you use an actual HTML parser to handle the data, and not rely on regular expressions. The following code for Python 2 and 3 produces a Unicode value with the textual information from your URL:

from bs4 import BeautifulSoup
try:
    from urllib import unquote
except ImportError:
    from urllib.parse import unquote

soup = BeautifulSoup(unquote(special_text), 'html.parser')  # consider installing lxml instead
text = soup.get_text('\n', strip=True)  # put newlines between sections
print(text)

For your input, on my Mac OSX terminal configured for handling Unicode text as UTF-8, I see:

愛迪達 adidas Energy Boost 路跑 低筒 運動 休閒 跑鞋 跑步 慢跑 馬拉松 健身房 流行 球鞋 女裝 女款 女 女鞋
*靈活、輕量、舒適兼具的選擇
*簡約現代的產品設計,年輕多樣化的配色方案,更為符合年輕消費者的審美偏好
*簡單的線條和乾淨的設計,提供了獨特的穿搭組合
*透氣與保護性,結合了ADIDAS的創新科技,創造了完美的產品
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I already can get Unicode characters just by doing unquote in my local environment, which is running on Mac. The problem is when I ran it on scrapinghub.com, I guess is their environment issues? But for non quoted text it shows proper Chinese characters. – forestclown Feb 06 '17 at 13:15
  • 1
    @forestclown: so how does scrapinghub.com give you that data. In a webpage? In a file? I don't know how that site works. It's *not a Python issue*, for sure. – Martijn Pieters Feb 06 '17 at 13:23
  • Thanks for pointing the problem out, I will try using bs4 (if I can install) and see what happens. To answer your question, scrapinghub allows you to show results on browser or download as text file, both showing garbaged if converted from unquote(), but for other text that didn't use unquote show up just fine. – forestclown Feb 06 '17 at 14:12
  • 1
    @forestclown: how did you open the text file? Does the browser page include encoding information (look for a `charset` parameter in the Content-Type header, or a [`` header declaring the encoding in HTML](https://www.w3.org/International/questions/qa-html-encoding-declarations.en). If there is no such hint, your computer could have picked the wrong codec. In other words, you may well still have UTF-8 but the way you then process the data uses the wrong codec. – Martijn Pieters Feb 06 '17 at 14:20
  • I am just wondering,
    Normal Chinese Text
    QUOTED Text
    , the full text contains both quoted and unquoted text. For class="a", it displays the Chinese text correctly, for class="b", I use unquote('QUOTED Text') and it display garbaged, does it still implies the codec is wrong?
    – forestclown Feb 06 '17 at 15:12
  • 1
    No, the URL quoting encoded all bytes using a scheme that only uses ASCII characters, with `%` and hexadecimal digits (0 - 9 and A - F). Almost all modern codecs are supersets of ASCII, including UTF-8 and CP1254, so you'll see those rendered just fine. `%` is encoded to the exact same number in UTF-8, CP1254 and in ASCII, so using the wrong codec can still result in 'mostly' readable text. Just not for Chinese. – Martijn Pieters Feb 06 '17 at 15:19
0

I don't know why, but for some reason I get it to work on scrapinghub.com like below.

Let say I have an HTML text like:

<html>
<div class="a">
   Some chinese text
</div>
<div class="b">
   QUOTED text got chinese in it
   %3Ctable%20width%3D%22100%25%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23ff0000%3Bfont-size%3A20pt%3Btext-align%3Acenter%3Bfont-weight%3Abold%22%3E%0D%0A%09%E6%84%9B%E8%BF%AA%E9%81%94%20adidas%20Energy%20Boost%20%E8%B7%AF%E8%B7%91%20%E4%BD%8E%E7%AD%92%20%E9%81%8B%E5%8B%95%20%E4%BC%91%E9%96%92%20%E8%B7%91%E9%9E%8B%20%E8%B7%91%E6%AD%A5%20%E6%85%A2%E8%B7%91%20%E9%A6%AC%E6%8B%89%E6%9D%BE%20%E5%81%A5%E8%BA%AB%E6%88%BF%20%E6%B5%81%E8%A1%8C%20%E7%90%83%E9%9E%8B%20%E5%A5%B3%E8%A3%9D%20%E5%A5%B3%E6%AC%BE%20%E5%A5%B3%20%E5%A5%B3%E9%9E%8B%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23000000%3Bfont-size%3A14pt%3Btext-align%3Acenter%22%3E%0D%0A%09%EF%BC%8A%E9%9D%88%E6%B4%BB%E3%80%81%E8%BC%95%E9%87%8F%E3%80%81%E8%88%92%E9%81%A9%E5%85%BC%E5%85%B7%E7%9A%84%E9%81%B8%E6%93%87%3Cbr%20%2F%3E%EF%BC%8A%E7%B0%A1%E7%B4%84%E7%8F%BE%E4%BB%A3%E7%9A%84%E7%94%A2%E5%93%81%E8%A8%AD%E8%A8%88%2C%E5%B9%B4%E8%BC%95%E5%A4%9A%E6%A8%A3%E5%8C%96%E7%9A%84%E9%85%8D%E8%89%B2%E6%96%B9%E6%A1%88%2C%E6%9B%B4%E7%82%BA%E7%AC%A6%E5%90%88%E5%B9%B4%E8%BC%95%E6%B6%88%E8%B2%BB%E8%80%85%E7%9A%84%E5%AF%A9%E7%BE%8E%E5%81%8F%E5%A5%BD%3Cbr%20%2F%3E%EF%BC%8A%E7%B0%A1%E5%96%AE%E7%9A%84%E7%B7%9A%E6%A2%9D%E5%92%8C%E4%B9%BE%E6%B7%A8%E7%9A%84%E8%A8%AD%E8%A8%88%2C%E6%8F%90%E4%BE%9B%E4%BA%86%E7%8D%A8%E7%89%B9%E7%9A%84%E7%A9%BF%E6%90%AD%E7%B5%84%E5%90%88%3Cbr%20%2F%3E%EF%BC%8A%E9%80%8F%E6%B0%A3%E8%88%87%E4%BF%9D%E8%AD%B7%E6%80%A7%2C%E7%B5%90%E5%90%88%E4%BA%86ADIDAS%E7%9A%84%E5%89%B5%E6%96%B0%E7%A7%91%E6%8A%80%2C%E5%89%B5%E9%80%A0%E4%BA%86%E5%AE%8C%E7%BE%8E%E7%9A%84%E7%94%A2%E5%93%81%3Cbr%20%2F%3E%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cdiv%20align%3D%22center%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fs.yimg.com%2Fwb%2Fimages%2F2B558E585E39649599A9A266349EABD17A4ABC18%22%20%2F%3E%3C%2Fdiv%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Ftable%3E%3Ctable%20width%3D%22100%25%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23000000%3Bfont-size%3A12pt%3Btext-align%3Aleft%3Bfont-weight%3A100%22%3E%0D%0A%09%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cdiv%20align%3D%22center%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fs.yimg.com%2Fwb%2Fimages%2F0F1A6CBFE6F6631189D491A17A2A2E7C388F194E%22%20%2F%3E%3Cdiv%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Ftable%3E%3Ctable%20width%3D%22100%25%22%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cp%20style%3D%22color%3A%23000000%3Bfont-size%3A12pt%3Btext-align%3Aleft%3Bfont-weight%3A100%22%3E%0D%0A%09%0D%0A%3C%2Fp%3E%0D%0A%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3Cdiv%20align%3D%22center%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fs.yimg.com%2Fwb%2Fimages%2FA0C9B09CAC784E2CA81A572E8F9F2E5721812607%22%20%2F%3E%3Cdiv%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3Ctr%3E%3Ctd%3E%3C%2Ftd%3E%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%2Ftable%3E
</div>
</html>

So I parse it to assign class="a" to variable AAA, class="b" to variable BBB

if I want to unquote BBB and have the chinese characters display correctly I do the following:

BBB = u' '.join((BBB, ''))
BBB = BBB.encode('ascii')
BBB = unquote(BBB)

So when I output both AAA & BBB on scrapinghub, it will both display chinese text correctly.

I just want to point out that Martijn Pieters is also correct in his answers when I am doing this locally on my MAC. But just not sure whats going on in scrapinghub that I need to do the above.

forestclown
  • 1,582
  • 4
  • 25
  • 39
  • 1
    Your `u' '.join()` call is a very verbose way of adding a space to the end of `BBB`, not sure why you are doing that. As for scrapinghub.com, have you tried contacting their support to ask them how they determine codecs? I strongly suspect they are using automatic detection of encodings (which can easily fail). – Martijn Pieters Feb 06 '17 at 15:38
  • scrapinghub is using Scrapy library (pip install Scrapy) to get text from CSS, so to get text from class="b" I need to do BBB = response.css("div.b"), where response stores the entire HTML. If I simply do BBB.encode() it gives me "TypeError: cannot use a string pattern on a bytes-like object" so I read somewhere to do BBB = u' '.join(BBB, '')) – forestclown Feb 06 '17 at 15:52
  • 1
    Oh dear. So it'll **decode** to Unicode if it isn't already. `if not isinstance(BBB, unicode): BBB = BBB.decode('ASCII')` would be a more explicit method, without having to second-guess what it is trying to do. – Martijn Pieters Feb 06 '17 at 15:55
  • Crawling for data is not my strongest area, I just need something quick to crawl data periodically to test my Machine Learning codes instead of spending time to setup something for it, but instead spending whole day to find out what is wrong with unicode conversion :) – forestclown Feb 06 '17 at 15:57
  • @MartijnPieters Thanks for all the help and support, the info you provided helps me a lot in fixing this – forestclown Feb 06 '17 at 15:58
  • 1
    You may want to read up on Unicode and Python then, I *strongly* recommend http://nedbatchelder.com/text/unipain.html here. – Martijn Pieters Feb 06 '17 at 15:59
  • 1
    For some reason, I cannot post my answer (StackOverflow thinks it's spam): see https://gist.github.com/redapple/1eb782f4e39ab79f7fdd841ad16ea052 – paul trmbrth Feb 07 '17 at 13:15
  • Thanks for the answer, appreciate it very much Hope you can post it as answer, as your answer should be the correct answer – forestclown Feb 08 '17 at 04:04