W
W
Weishaupt2022-03-16 21:18:26
Python
Weishaupt, 2022-03-16 21:18:26

How to use regex to translate (googletrans) the desired part of a string in Python?

Can't embed regex and translate headers in response without affecting project name

Regular expression itself:
(?<=\w+: ).+(?=)

Generated code for Python:

import re
pattern = re.compile(ur'(?<=\w+: ).+(?=)')
str = u''
print(pattern.search(str))


A little about googletrans:
translate(text, dest='ru', src='en')
text - Source text to translate.
src - Language of the source text
dest - Language for translating the source text.

Sources:
from bs4 import BeautifulSoup
import requests
from googletrans import Translator
import re

x = 0
while True:
    if x == 0:
        url = "https://news.ycombinator.com/newest"
    else:
        url = "https://news.ycombinator.com/newest" + nexx

    request = requests.get(url)
    soup = BeautifulSoup(request.text, "html.parser")
    teme = soup.find_all("td", class_="title")

    for temes in teme:
        temes = temes.find("a", {'class' : 'titlelink'})
        if temes is not None and 'github.com' in str(temes):
            sublink = temes.get('href')
            print(str(temes.text) + ' ' + str(sublink))
            print('===')

    nex = soup.find(class_ = 'morelink')
    nexlink = nex.get('href')
    nexx = nexlink[6:]
    x += 1

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question