change the language requests_html python

66 Views Asked by At

I have one site trying to scarp it using requests_html

but it only take Arabic language I need the English text of the title Etc..

import pandas
from requests_html import HTMLSession
import time
import requests
from requests import get
from requests_html import HTMLSession
import re


url = 'https://pcpalace.com.sa/products/ASUS-Vivobook-GO-E1504GA'

headers = {"Accept-Language": "en"}

session = HTMLSession()
r = requests.get(url, headers=headers)
r.raise_for_status()

#name
makestr=str(r.text)
name=makestr.splitlines()[50]
print(name)

I tried using

headers = {"Accept-Language": "en"}

but that was not working either

1

There are 1 best solutions below

2
Brentspine On

The Accept-Language header is only used to inform the server about the preferred language of the response. It doesn't guarantee that the server will respond in the specified language, but only if available.

You can try translating the specific text you want by using the Google Translate python library. Here's an example on how to translate the title element of the response:

from googletrans import Translator

[...]

# Finds the title element using .find()
title_element = r.html.find('title', first=True)

# Extracts the text from the element
title_text = title_element.text if title_element else 'Title not found'

# Translates the title to English
translated_title = translate_text(title_text)

print("Original Title: " + title_text)
print("Translated Title: " + translated_title)

Install Google Translate 4 using pip:

pip install googletrans==4.0.0-rc1

4.0.0rc1 is a prerelease version, if for some reason it doesn't work try using the 3.0 version:

pip uninstall googletrans==4.0.0-rc1
pip install googletrans==3.0.0