python - 如何从 html 将结果返回到表或 csv 类型的格式

标签 python python-3.x beautifulsoup

我正在尝试从一个可以在 NFL 赛季期间运行的投注网站上获取赔率,以便将赔率存入 Excel/DB,但由于我对 python 和 bs4 非常陌生,所以我遇到了麻烦。

我正在使用带有 BS4 的 Python 3.7.4

import requests
from bs4 import BeautifulSoup
result2 = requests.get("https://www.betfair.com/sport/american-football/nfl-kampe/green-bay-packers-chicago-bears/29202049")
src2 = result2.content
soup = BeautifulSoup(src2, 'lxml')

for item in soup.find_all('div', {'class': 'minimarketview-content'}):
    print(item.text)

我希望输出是这样的 csv:

"Green Bay Packers", "2.3", "Chicago Bears", "1.55"
"Green Bay Packers", "1.7","+3,5", "Chicago Bears", "2.0","-3.5"

当前结果(有大换行符):

Green Bay Packers

2.3

Chicago Bears

1.55

Green Bay Packers

1.7

+3,5

etc

最佳答案

我无法访问该网站,因为它被我所在的公共(public) wifi 的防火墙阻止,因此我无法测试下面的代码,但不要打印项目,而是将它们放入列表中。然后获取该列表并将其转换为数据框/表。所以类似:

注意:清理工作仍有待完成,但这会让你继续前进

import requests
from bs4 import BeautifulSoup
import pandas as pd

result2 = requests.get("https://www.betfair.com/sport/american-football/nfl-kampe/green-bay-packers-chicago-bears/29202049")
src2 = result2.content
soup = BeautifulSoup(src2, 'lxml')

data = []
for item in soup.find_all('div', {'class': 'minimarketview-content'}):
    temp_data = [ alpha for alpha in item.text.split('\n') if alpha != '' ] 
    data.append(temp_data)

df = pd.DataFrame(data)
print(df)

df.to_csv('file.csv')

输出:

print (df.to_string())
                               0     1                      2              3                          4      5                  6     7
0              Green Bay Packers  11/8          Chicago Bears           8/13                       None   None               None  None
1              Green Bay Packers   3/4                   +3.5  Chicago Bears                      11/10   -3.5               None  None
2                Current Points:  Over                  20/23            +46                      Under  19/20                +46  None
3  Green Bay Packers by 1-13 Pts   2/1  Green Bay Packers 14+            5/1  Chicago Bears by 1-13 Pts    6/4  Chicago Bears 14+  10/3

关于python - 如何从 html 将结果返回到表或 csv 类型的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57692156/

相关文章:

python - BeautifulSoup get_text() 函数包括 css

python - pytest assert_used_with 不适用于 Django 管理命令

python - 如何删除列表中特定字符之后的所有字符

python - Pandas - 过滤列至少匹配一次的行

python - 如何在 python 中的用户定义函数中实现 "positional-only parameter"?

python - 无法使用 Beautiful Soup 下载所有图像

Python 使用 'consist of' 搜索列表值

python - 当找到 list2 中的项目时移动到 list1 中的下一个项目,当比较两个列表时

python - 如何比较两个列表列表之间的对应位置

python - Selenium Web 在动态内容和隐藏数据表上使用 Beautiful Soup 进行抓取