【发布时间】:2019-01-12 14:43:51
【问题描述】:
作为对我自己的挑战,我正在尝试从谷歌翻译中抓取翻译。我通过浏览器模拟请求下载了 html 文件,然后使用 beautifulSoupe 4 我尝试在 html 代码中查找翻译。问题是,虽然我可以在 Chrome WebDev 模式下找到我想要的东西,但我无法在 Python 中或使用其他程序(如 noteblock)来做这样的事情。这是我的代码:
while Translating==True:
text=input(("Values to translate:"))
#CREATING URL TO SCRAPE
translator_url_with_translation_embebed= translator_url+ EncodeTexttoURL(text)
#REQUESTING PAGE WITH FAKE HEADERS (avoiding no browser detection)
url = translator_url_with_translation_embebed
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response= requests.get(url, headers=headers)
print(response.content)
#ANALISING RESPONSE FOR FURTHER USE
sopa = BeautifulSoup(response.content)
translation_location=sopa.find_all('span', {'class' : 'tlid-translation translation'})
print("TRANSLATED LOCATIOON")
print(translation_location
` translation_location 输出一个空列表。
在 html 代码中显示翻译的浏览器开发模式:
我怎样才能找到它们?其他地方有翻译吗?谷歌是否发送了他们网站的修改版本来愚弄我?!
感谢您的帮助
【问题讨论】:
-
print(response.text)提供了什么? -
我想你会觉得这很有趣:pypi.org/project/deep-translator
标签: python-3.x web-scraping beautifulsoup