刮痧表&打印各种数据

时间:2017-01-18 05:27:55

标签: python beautifulsoup python-requests

我目前有以下代码行

import requests, re, bs4
from urllib.parse import urljoin
start_url = 'http://www.racingaustralia.horse/'

def make_soup(url):
    r = requests.get(url)
    soup = bs4.BeautifulSoup(r.text,"lxml")
    return soup

def get_links(url):
    soup = make_soup(url)
    a_tags = soup.find_all('a', href=re.compile(r"^/FreeFields/"))
    links = [urljoin(start_url,a['href']) for a in a_tags]
return links

def get_tds(link):
    soup = make_soup(link)
    tds = soup.find_all('td', class_="horse")
    for td in tds:
            print(td.text)

if __name__ == '__main__':
    links = get_links(start_url)
    for link in links:
        get_tds(link)

这会使来自racingaustralia.com/horse

的桌面会议的所有马名称全部删除

这正是我想要的,但我也想要检索会议日期,会议地点和每场比赛,列出马名。

这是我想要的一个例子:

Date of Race Meet
Location of Race Meet
Race Number
Horse....
...
...
...
Race Number
Horse
...
...
etc

是否有人能够帮我调整代码,以便打印每场比赛的日期和地点以及每匹马的比赛号码?

我尝试了以下方法,但我想知道是否有更有效的方法。

def get_tds(link):
    soup = make_soup(link)
    race_date = soup.find_all('span', class_="race-venue-date")
    for span in race_date:
        print(span.text)

    tds = soup.find_all('td', class_="horse")
    for td in tds:
        print(td.text)

def get_info(link):
    item = soup.find_all('div', class_="top")
    for div in item:
        print(div.text)

if __name__ == '__main__':
    links = get_links(start_url)
    for link in links:
        get_info(link), get_tds(link)

提前致谢

1 个答案:

答案 0 :(得分:0)

import requests, re, bs4
from urllib.parse import urljoin


def make_soup(url):
    r = requests.get(url)
    soup = bs4.BeautifulSoup(r.text,"lxml")
    return soup

def get_links(url):
    soup = make_soup(url)
    a_tags = soup.find_all('a', href=re.compile(r"^/FreeFields/"))
    links = [urljoin(start_url,a['href']) for a in a_tags]
    return links

def get_info(link):
    soup = make_soup(link)
    tds = soup.find_all('td', class_="horse")
    if tds:
        top = soup.find(class_="top").h2
        for s in top.stripped_strings:
            print(s)
        for index, td in enumerate(tds, 1):
            print(index, td.text, sep='\n')
    else:
        print('not find')

if __name__ == '__main__':
    start_url = 'http://www.racingaustralia.horse/'
    links = get_links(start_url)
    for link in links:
        get_info(link)

出:

Warwick Farm: Australian Turf Club
Wednesday, 18 January 2017
1
GAUGUIN (NZ) 
2
DAHOOIL (NZ) 
3
METAMORPHIC 
4
MY KIND 
5
CONCISELY 
6
ARAZONA 
7
APOLLO 
8
IGNITE THE LIGHT 
9
KRUPSKAYA 

有很多网址不包含您需要的信息,您应该更改正则表达式以对其进行过滤,这样您的代码就可以更快地运行。