bs4返回dom对象包含的html,用bs4抓取网页

import requests

from bs4 import BeautifulSoup

url = "http://bet.hkjc.com/football/index.aspx?lang=en"

r = requests.get(url)

soup = BeautifulSoup(r.content, "html.parser")

div = soup.find("div", {"class": "footballmaincontent"})

tables = div.find_all("table")

my_table = tables[2]

for row in my_table.find_all('tr'):

cols = row.find_all('td')

odds_list = []

if len(cols) >= 10:

match_no = (cols[0].text.strip())

teams = (cols[2].text.strip())

match_time = (cols[4].text.strip())

home_odds = (cols[7].text.strip())

away_odds = (cols[8].text.strip())

draw_odds = (cols[9].text.strip())

odds_row = (match_no,teams,match_time,home_odds,away_odds,draw_odds)

odds_list.append(odds_row)

# Write to csv file

import csv

with open("odds_file.csv", "wb") as file:

writer = csv.writer(file)

for row in odds_list:

writer.writerow(row)

我试图通过将列附加到for循环中的“odds_list”来将列导出到csv文件。但事实证明,它没有在“odds_file”中写入任何内容。用bs4抓取网页

我知道那里有与

odds_row = (match_no,teams,match_time,home_odds,away_odds,draw_odds)

但我怎么能追加,我到CSV文件的上榜出问题了吗?

2016-09-02

Y Y

+0

使用儿童[2]你选择DOM中的第3个表。这是否正是你想要的,DOM中的数据fron第三表? –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值