相关疑难解决方法(0)

通过谷歌翻译翻译的Python脚本

我正在尝试学习python,所以我决定编写一个可以使用谷歌翻译翻译的脚本.直到现在我写了这个:

import sys
from BeautifulSoup import BeautifulSoup
import urllib2
import urllib

data = {'sl':'en','tl':'it','text':'word'} 
request = urllib2.Request('http://www.translate.google.com', urllib.urlencode(data))

request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11')
opener = urllib2.build_opener()
feeddata = opener.open(request).read()
#print feeddata
soup = BeautifulSoup(feeddata)
print soup.find('span', id="result_box")
print request.get_method()
Run Code Online (Sandbox Code Playgroud)

而现在我被卡住了.我看不到它中的任何错误,但它仍然无效(我的意思是脚本将运行,但它不会翻译这个词).

有谁知道如何修理它?(抱歉我的英语不好)

python urllib2 beautifulsoup google-translate

4
推荐指数
3
解决办法
1万
查看次数

如何用谷歌翻译器翻译单词?

我正在制作一个简单的脚本,使用 requests 和 BeatifulSoup 将单词从英语翻译成俄语,问题是结果框是空的,应该翻译单词/我不确定是否应该使用 GET 或 POST 方法。这是我尝试过的

with open('File.csv', 'r') as file:
    csv_reader = csv.reader(file)

    for line in csv_reader:
        if line[1] == '':

            url = 'https://translate.google.com/#en/ru/{}'.format(line[0])
            r = requests.get(url, timeout=5)
            soup = BeautifulSoup(r.content, 'html.parser')

            translate = soup.find('span', id='result_box')
            for word in translate:
                print(word.find('span', class_=''))
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup

1
推荐指数
1
解决办法
2601
查看次数

标签 统计

beautifulsoup ×2

python ×2

google-translate ×1

urllib2 ×1