4

I am trying to create a txt single file containing the entire text of the Torah. The best source I could find on google is: http://www.mechon-mamre.org/i/t/t0101.htm.

This gives me chapters which I would have to concatenate and write out to my file.

Using urxvt (a terminal with unicode support) when running the following script I get gibberish:

import requests
from bs4 import BeautifulSoup

url = 'http://www.mechon-mamre.org/i/t/t0101.htm'
response = requests.get(url)
soup = BeautifulSoup(response.text)
paragraphs = soup.find_all('p')

print paragraphs[1]

Outputs:

<p><b>à</b> <big>áÌÀ</big>øÅàùÑÄéú, áÌÈøÈà àÁìÉäÄéí, àÅú äÇùÌÑÈîÇéÄí, åÀàÅú äÈàÈøÆõ. 
<a name="2"> </a>
<b>á</b> åÀäÈàÈøÆõ, äÈéÀúÈä úÉäåÌ åÈáÉäåÌ, åÀçÉùÑÆêÀ, òÇì-ôÌÀðÅé úÀäåÉí; åÀøåÌçÇ àÁìÉäÄéí, îÀøÇçÆôÆú òÇì-ôÌÀðÅé äÇîÌÈéÄí. 
.....

I tried to force the output to use unicode by default but I got the exact same output:

print unicode(paragraphs[1])

Both BeautfulSoup and Python-Requests claim to handle unicode, so the issue is probably with my code. If I hand-paste the hebrew text from the browser to urxvt, I get actual hebrew. Therefore the terminal is not the problem.

TheOne
  • 10,819
  • 20
  • 81
  • 119
  • What are you trying to print to? If it is the Windows Command Prompt you might as well give up now. – bobince Sep 17 '13 at 20:03
  • @bobince, I mention urxvt in the question. – TheOne Sep 17 '13 at 20:04
  • Ah, OK. Well, the above is cp1255 Hebrew misinterpreted as ISO-8859-1. Unfortunately the page you link above fails to specify a `charset` in the `Content-Type` header, so the `requests` module can't know what encoding it is, and guesses wrong; real browsers often guess wrong too. I think in Requests if you override the `response.encoding='cp1255'` that should change what you get back from `response.text` suitably. – bobince Sep 17 '13 at 20:10

1 Answers1

6

The server does not report the encoding of the content, and requests (or rather chardet) mis-guesses the content encoding.

Use the raw bytes instead:

>>> soup = BeautifulSoup(response.content)
>>> paragraphs = soup.find_all('p')
>>> print paragraphs[1]
<p><b>א</b> <big>בְּ</big>רֵאשִׁית, בָּרָא אֱלֹהִים, אֵת הַשָּׁמַיִם, וְאֵת הָאָרֶץ. 
<a name="2"> </a>
<b>ב</b> וְהָאָרֶץ, הָיְתָה תֹהוּ וָבֹהוּ, וְחֹשֶׁךְ, עַל-פְּנֵי תְהוֹם; וְרוּחַ אֱלֹהִים, מְרַחֶפֶת עַל-פְּנֵי הַמָּיִם. 
<a name="3"> </a>
<b>ג</b> וַיֹּאמֶר אֱלֹהִים, יְהִי אוֹר; וַיְהִי-אוֹר. 
<a name="4"> </a>
<b>ד</b> וַיַּרְא אֱלֹהִים אֶת-הָאוֹר, כִּי-טוֹב; וַיַּבְדֵּל אֱלֹהִים, בֵּין הָאוֹר וּבֵין הַחֹשֶׁךְ. 
<a name="5"> </a>
<b>ה</b> וַיִּקְרָא אֱלֹהִים לָאוֹר יוֹם, וְלַחֹשֶׁךְ קָרָא לָיְלָה; וַיְהִי-עֶרֶב וַיְהִי-בֹקֶר, יוֹם אֶחָד.  {פ}<br/>
<a name="6"> </a>
<b>ו</b> וַיֹּאמֶר אֱלֹהִים, יְהִי רָקִיעַ בְּתוֹךְ הַמָּיִם, וִיהִי מַבְדִּיל, בֵּין מַיִם לָמָיִם. 
<a name="7"> </a>
<b>ז</b> וַיַּעַשׂ אֱלֹהִים, אֶת-הָרָקִיעַ, וַיַּבְדֵּל בֵּין הַמַּיִם אֲשֶׁר מִתַּחַת לָרָקִיעַ, וּבֵין הַמַּיִם אֲשֶׁר מֵעַל לָרָקִיעַ; וַיְהִי-כֵן. 
<a name="8"> </a>
<b>ח</b> וַיִּקְרָא אֱלֹהִים לָרָקִיעַ, שָׁמָיִם; וַיְהִי-עֶרֶב וַיְהִי-בֹקֶר, יוֹם שֵׁנִי.  {פ}<br/>
<a name="9"> </a>
<b>ט</b> וַיֹּאמֶר אֱלֹהִים, יִקָּווּ הַמַּיִם מִתַּחַת הַשָּׁמַיִם אֶל-מָקוֹם אֶחָד, וְתֵרָאֶה, הַיַּבָּשָׁה; וַיְהִי-כֵן. 
<a name="10"> </a>
<b>י</b> וַיִּקְרָא אֱלֹהִים לַיַּבָּשָׁה אֶרֶץ, וּלְמִקְוֵה הַמַּיִם קָרָא יַמִּים; וַיַּרְא אֱלֹהִים, כִּי-טוֹב. 
<a name="11"> </a>
<b>יא</b> וַיֹּאמֶר אֱלֹהִים, תַּדְשֵׁא הָאָרֶץ דֶּשֶׁא עֵשֶׂב מַזְרִיעַ זֶרַע, עֵץ פְּרִי עֹשֶׂה פְּרִי לְמִינוֹ, אֲשֶׁר זַרְעוֹ-בוֹ עַל-הָאָרֶץ; וַיְהִי-כֵן. 
<a name="12"> </a>
<b>יב</b> וַתּוֹצֵא הָאָרֶץ דֶּשֶׁא עֵשֶׂב מַזְרִיעַ זֶרַע, לְמִינֵהוּ, וְעֵץ עֹשֶׂה-פְּרִי אֲשֶׁר זַרְעוֹ-בוֹ, לְמִינֵהוּ; וַיַּרְא אֱלֹהִים, כִּי-טוֹב. 
<a name="13"> </a>
<b>יג</b> וַיְהִי-עֶרֶב וַיְהִי-בֹקֶר, יוֹם שְׁלִישִׁי.  {פ}<br/>
<a name="14"> </a>
<b>יד</b> וַיֹּאמֶר אֱלֹהִים, יְהִי מְאֹרֹת בִּרְקִיעַ הַשָּׁמַיִם, לְהַבְדִּיל, בֵּין הַיּוֹם וּבֵין הַלָּיְלָה; וְהָיוּ לְאֹתֹת וּלְמוֹעֲדִים, וּלְיָמִים וְשָׁנִים. 
<a name="15"> </a>
<b>טו</b> וְהָיוּ לִמְאוֹרֹת בִּרְקִיעַ הַשָּׁמַיִם, לְהָאִיר עַל-הָאָרֶץ; וַיְהִי-כֵן. 
<a name="16"> </a>
<b>טז</b> וַיַּעַשׂ אֱלֹהִים, אֶת-שְׁנֵי הַמְּאֹרֹת הַגְּדֹלִים:  אֶת-הַמָּאוֹר הַגָּדֹל, לְמֶמְשֶׁלֶת הַיּוֹם, וְאֶת-הַמָּאוֹר הַקָּטֹן לְמֶמְשֶׁלֶת הַלַּיְלָה, וְאֵת הַכּוֹכָבִים. 
<a name="17"> </a>
<b>יז</b> וַיִּתֵּן אֹתָם אֱלֹהִים, בִּרְקִיעַ הַשָּׁמָיִם, לְהָאִיר, עַל-הָאָרֶץ. 
<a name="18"> </a>
<b>יח</b> וְלִמְשֹׁל, בַּיּוֹם וּבַלַּיְלָה, וּלְהַבְדִּיל, בֵּין הָאוֹר וּבֵין הַחֹשֶׁךְ; וַיַּרְא אֱלֹהִים, כִּי-טוֹב. 
<a name="19"> </a>
<b>יט</b> וַיְהִי-עֶרֶב וַיְהִי-בֹקֶר, יוֹם רְבִיעִי.  {פ}<br/>
<a name="20"> </a>
<b>כ</b> וַיֹּאמֶר אֱלֹהִים--יִשְׁרְצוּ הַמַּיִם, שֶׁרֶץ נֶפֶשׁ חַיָּה; וְעוֹף יְעוֹפֵף עַל-הָאָרֶץ, עַל-פְּנֵי רְקִיעַ הַשָּׁמָיִם. 
<a name="21"> </a>
<b>כא</b> וַיִּבְרָא אֱלֹהִים, אֶת-הַתַּנִּינִם הַגְּדֹלִים; וְאֵת כָּל-נֶפֶשׁ הַחַיָּה הָרֹמֶשֶׂת אֲשֶׁר שָׁרְצוּ הַמַּיִם לְמִינֵהֶם, וְאֵת כָּל-עוֹף כָּנָף לְמִינֵהוּ, וַיַּרְא אֱלֹהִים, כִּי-טוֹב. 
<a name="22"> </a>
<b>כב</b> וַיְבָרֶךְ אֹתָם אֱלֹהִים, לֵאמֹר:  פְּרוּ וּרְבוּ, וּמִלְאוּ אֶת-הַמַּיִם בַּיַּמִּים, וְהָעוֹף, יִרֶב בָּאָרֶץ. 
<a name="23"> </a>
<b>כג</b> וַיְהִי-עֶרֶב וַיְהִי-בֹקֶר, יוֹם חֲמִישִׁי.  {פ}<br/>
<a name="24"> </a>
<b>כד</b> וַיֹּאמֶר אֱלֹהִים, תּוֹצֵא הָאָרֶץ נֶפֶשׁ חַיָּה לְמִינָהּ, בְּהֵמָה וָרֶמֶשׂ וְחַיְתוֹ-אֶרֶץ, לְמִינָהּ; וַיְהִי-כֵן. 
<a name="25"> </a>
<b>כה</b> וַיַּעַשׂ אֱלֹהִים אֶת-חַיַּת הָאָרֶץ לְמִינָהּ, וְאֶת-הַבְּהֵמָה לְמִינָהּ, וְאֵת כָּל-רֶמֶשׂ הָאֲדָמָה, לְמִינֵהוּ; וַיַּרְא אֱלֹהִים, כִּי-טוֹב. 
<a name="26"> </a>
<b>כו</b> וַיֹּאמֶר אֱלֹהִים, נַעֲשֶׂה אָדָם בְּצַלְמֵנוּ כִּדְמוּתֵנוּ; וְיִרְדּוּ בִדְגַת הַיָּם וּבְעוֹף הַשָּׁמַיִם, וּבַבְּהֵמָה וּבְכָל-הָאָרֶץ, וּבְכָל-הָרֶמֶשׂ, הָרֹמֵשׂ עַל-הָאָרֶץ. 
<a name="27"> </a>
<b>כז</b> וַיִּבְרָא אֱלֹהִים אֶת-הָאָדָם בְּצַלְמוֹ, בְּצֶלֶם אֱלֹהִים בָּרָא אֹתוֹ:  זָכָר וּנְקֵבָה, בָּרָא אֹתָם. 
<a name="28"> </a>
<b>כח</b> וַיְבָרֶךְ אֹתָם, אֱלֹהִים, וַיֹּאמֶר לָהֶם אֱלֹהִים פְּרוּ וּרְבוּ וּמִלְאוּ אֶת-הָאָרֶץ, וְכִבְשֻׁהָ; וּרְדוּ בִּדְגַת הַיָּם, וּבְעוֹף הַשָּׁמַיִם, וּבְכָל-חַיָּה, הָרֹמֶשֶׂת עַל-הָאָרֶץ. 
<a name="29"> </a>
<b>כט</b> וַיֹּאמֶר אֱלֹהִים, הִנֵּה נָתַתִּי לָכֶם אֶת-כָּל-עֵשֶׂב זֹרֵעַ זֶרַע אֲשֶׁר עַל-פְּנֵי כָל-הָאָרֶץ, וְאֶת-כָּל-הָעֵץ אֲשֶׁר-בּוֹ פְרִי-עֵץ, זֹרֵעַ זָרַע:  לָכֶם יִהְיֶה, לְאָכְלָה. 
<a name="30"> </a>
<b>ל</b> וּלְכָל-חַיַּת הָאָרֶץ וּלְכָל-עוֹף הַשָּׁמַיִם וּלְכֹל רוֹמֵשׂ עַל-הָאָרֶץ, אֲשֶׁר-בּוֹ נֶפֶשׁ חַיָּה, אֶת-כָּל-יֶרֶק עֵשֶׂב, לְאָכְלָה; וַיְהִי-כֵן. 
<a name="31"> </a>
<b>לא</b> וַיַּרְא אֱלֹהִים אֶת-כָּל-אֲשֶׁר עָשָׂה, וְהִנֵּה-טוֹב מְאֹד; וַיְהִי-עֶרֶב וַיְהִי-בֹקֶר, יוֹם הַשִּׁשִּׁי.  {פ}</p>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for that! I switched from a different source which did have the encoding set to unicode. – TheOne Sep 17 '13 at 20:12
  • So `requests` doesn't honor the encoding declaration in the file itself? That seems lame. – BrenBarn Sep 17 '13 at 20:14
  • @BrenBarn: There is **no** file; there are only HTTP headers. And there is no encoding declared in the Content-Type header, so all `requests` can *do* is guess. – Martijn Pieters Sep 17 '13 at 20:21
  • @MartijnPieters, can I tell `requests` which encoding to use? – TheOne Sep 17 '13 at 20:22
  • @Ramin: There is some discussion of this on the [`requests` bug tracker](https://github.com/kennethreitz/requests/pull/1589). [This comment](https://github.com/kennethreitz/requests/pull/1589#issuecomment-24230646) claims to provide a workaround. – BrenBarn Sep 17 '13 at 20:23