使用漂亮的汤获取特定div元素的子元素 [英] Getting the child element of a particular div element using beautiful soup

查看:122
本文介绍了使用漂亮的汤获取特定div元素的子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此链接中抓取表格数据

I am trying to scrape table data from this link

这是我的代码

from lxml import html
import webbrowser
import re
import xlwt
import requests
import bs4

content = requests.get("http://bet.hkjc.com/racing/pages/odds_wp.aspx?date=30-01-2017&venue=ST&raceno=1&lang=en").text # Get page content
soup = bs4.BeautifulSoup(content, 'lxml') # Parse page content 

table = soup.find('div', {'id': 'detailWPTable'}) # Locate that table tag

rows = table.find_all('tr') # Find all row tags in that table

for row in rows:
    columns = row.find_all('td') # Find all data tags in each column
    print ('\n')
    for column in columns:
        print (column.text.strip(),end=' ') # Output data in each column

它没有给出任何输出.请帮忙!

It is not giving any output . Please help !

推荐答案

您可以尝试使用 dryscrape 像这样:

You can try it with dryscrape like so:

import dryscrape
from bs4 import BeautifulSoup as BS
import re
import xlwt

ses=dryscrape.Session()
ses.visit("http://bet.hkjc.com/racing/pages/odds_wp.aspx?date=30-01-2017&venue=ST&raceno=1&lang=en")
soup = BS(ses.body(), 'lxml') # Parse page content 

table = soup.find('div', {'id': 'detailWPTable'}) # Locate that table tag

rows = table.find_all('tr') # Find all row tags in that table

for row in rows:
    columns = row.find_all('td') # Find all data tags in each column
    print ('\n')
    for column in columns:
        print (column.text.strip())

这篇关于使用漂亮的汤获取特定div元素的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆