Web scraping on specified table

My mission is scraping the first and the last record from the table and save to excel. Expected Result as below: ’07-28 03:17′, ‘3.90’, ‘1.97’, ‘2.75’ ’07-29 18:41′, ‘3.90’, ‘1.97’, ‘2.75’

Here are the code:

import pandas as pd
import datetime
import requests
from bs4 import BeautifulSoup
        
url = ('https://g10oal.com/match/116539/odds')
r = requests.get(url)
data = BeautifulSoup(r.text, 'lxml')
fha = data.findAll('table')[1] #半場主客和
file2 = open("c:/logs/link/G10oal-fha.txt","a+")
rows = fha.find_all('tr')
for row in rows:
    cols=row.find_all('td')
    cols=[x.text.strip() for x in cols]
    print(cols)
    file2.write(str(cols))
file2.close()

============================================================================ The problem was solved as below coding:

data = BeautifulSoup(r.content, 'html.parser')
    fha = data.findAll('table')[1] #半場主客和
    file3 = open("c:/logs/history/HKJC-FHA-2021-" + mth + day + ".txt","a+")
    rows = fha.find_all('tr')
    check_row = (len(rows))
    early_row = check_row - 1
    early_row = rows[early_row].text.split()
    last_row = rows[1].text.split()
    result = early_row, last_row
    print(link)
    print(result)
    file3.write(str(result) + "n")
    file3.close()

Answer

Maybe drop your data into a list and just grab the first and last?

data = BeautifulSoup(r.text, 'lxml')
result = [res for res in data.select('body > div.container.match-odds > div.match-info-header > div > div:nth-child(2) > div > p')]
print("First: %snLast: %s" % (result[0], restult[-1]))

Leave a Reply

Your email address will not be published. Required fields are marked *