如何使用Python抓取多语言网站

蜜汁

我正在使用Python从日语网站中抓取数据,该网站提供英语和日语两种语言。连结这里

问题是我得到了我需要的数据,但是使用了错误的语言(两种语言的链接相同)。我尝试检查html页面,并看到如下元素“ lang”:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja" class="">

这是我使用的代码:

import requests
import lxml.html as lh
import pandas as pd

url='https://data.j-league.or.jp/SFMS01/search?team_ids=33&home_away_select=0'
page = requests.get(url)
doc = lh.fromstring(page.content)
tr_elements = doc.xpath('//tr')
col = []
i = 0

for t in tr_elements[0]:
    i += 1
    name = t.text_content()
    print("{}".format(name))
    col.append((name,[]))

此时,我从页面获得了表格的首行,但为日语版本。我是Python的新手,而且还很熟。我不知道是否可以使用任何方法来获取英语数据?如果有任何我可以使用的示例,模板或其他资源,那会更好。

提前致谢!

布乔

我访问了您添加的网站,因此对于英语它添加了一个cookie(请查看“Request URL: https://data.j-league.or.jp/SFMS01/search?team_ids=33&home_away_select=0网络”标签中的标题),您将看到
Set-Cookie: SFCM01LANG=en; Max-Age=63072000; Expires=Tue, 18-Oct-2022 19:14:29 GMT; Path=/


所以我基本上已经使用了,将您的代码段更改为此

import requests
import lxml.html as lh
import pandas as pd

url='https://data.j-league.or.jp/SFMS01/search?team_ids=33&home_away_select=0'
page = requests.get(url, cookies={'SFCM01LANG':'en'})
doc = lh.fromstring(page.content)
tr_elements = doc.xpath('//tr')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章