【问题标题】:Scrape a page after translating it using bs4使用 bs4 翻译后抓取页面
【发布时间】:2019-03-02 04:26:11
【问题描述】:

我正在尝试通过将其转换为英文来抓取法国的页面。

这是我在 python 中使用漂亮的汤和请求包的代码。

import requests
from bs4 import BeautifulSoup
url = '<url>'
headers = {"Accept-Language": "en,en-gb;q=0.5"}
r = requests.get(url, headers=headers)
c = r.content
soup = BeautifulSoup(c)

但这仍然是法语文本。

任何人都可以建议更改/替代代码。

【问题讨论】:

    标签: python web-scraping beautifulsoup http-accept-language


    【解决方案1】:

    您可以利用TextBlob 将字符串转换为各种语言,这是从法语 ebay 网站转换跨度的示例:

    import requests
    from bs4 import BeautifulSoup
    from textblob import TextBlob
    
    url = 'https://www.ebay.fr/'
    french = []
    english = []
    r = requests.get(url)
    c = r.content
    soup = BeautifulSoup(c)
    for li in soup.find_all('span'):
        french.append(li.text)
    
    Frenchstr = ''.join(french)
    blob = TextBlob(Frenchstr)
    print(Frenchstr)
    Englishstr = blob.translate(to="EN")
    print('------------------------------------------------')
    print(Englishstr)
    

    【讨论】:

    • 谢谢,我会在报废的数据上试试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 2013-12-04
    • 2020-08-30
    相关资源
    最近更新 更多