Python 如何修改同一网站的新url的网页垃圾代码?
以下是工作代码:Python 如何修改同一网站的新url的网页垃圾代码?,python,pandas,selenium-webdriver,web-scraping,Python,Pandas,Selenium Webdriver,Web Scraping,以下是工作代码: browser = webdriver.Chrome() class GameData: def __init__(self): self.date = [] self.time = [] self.game = [] self.score = [] self.home_odds = [] self.draw_odds = [] self.away_odd
browser = webdriver.Chrome()
class GameData:
def __init__(self):
self.date = []
self.time = []
self.game = []
self.score = []
self.home_odds = []
self.draw_odds = []
self.away_odds = []
self.country = []
self.league = []
def parse_data(url):
browser.get(url)
df = pd.read_html(browser.page_source, header=0)[0]
html = browser.page_source
soup = bs(html, "lxml")
cont = soup.find('div', {'id': 'wrap'})
content = cont.find('div', {'id': 'col-content'})
content = content.find('table', {'class': 'table-main'}, {'id': 'tournamentTable'})
main = content.find('th', {'class': 'first2 tl'})
if main is None:
return None
count = main.findAll('a')
country = count[1].text
league = count[2].text
game_data = GameData()
game_date = None
for row in df.itertuples():
if not isinstance(row[1], str):
continue
elif ':' not in row[1]:
game_date = row[1].split('-')[0]
continue
game_data.date.append(game_date)
game_data.time.append(row[1])
game_data.game.append(row[2])
game_data.score.append(row[3])
game_data.home_odds.append(row[4])
game_data.draw_odds.append(row[5])
game_data.away_odds.append(row[6])
game_data.country.append(country)
game_data.league.append(league)
return game_data
#Input the URL here
urls = {
"https://www.oddsportal.com/soccer/england/premier-league/results/"
}
#Error handling incase website fails to load
if __name__ == '__main__':
results = None
for url in urls:
try:
game_data = parse_data(url)
if game_data is None:
continue
result = pd.DataFrame(game_data.__dict__)
if results is None:
results = result
else:
results = results.append(result, ignore_index=True)
except ValueError:
game_data = parse_data(url)
if game_data is None:
continue
result = pd.DataFrame(game_data.__dict__)
if results is None:
results = result
except AttributeError:
game_data = parse_data(url)
if game_data is None:
continue
result = pd.DataFrame(game_data.__dict__)
if results is None:
results = result
else:
results = results.append(result, ignore_index=True)
print(tabulate(results, headers='keys', tablefmt="github"))
| | date | time | game | score | home_odds | draw_odds | away_odds | country | league |
|----|-------------------|--------|----------------------------------|---------|-------------|-------------|-------------|-----------|----------------|
| 0 | Yesterday, 11 May | 19:15 | Southampton - Crystal Palace | 3:1 | 1.89 | 3.8 | 4.11 | England | Premier League |
| 1 | Yesterday, 11 May | 17:00 | Manchester Utd - Leicester | 1:2 | 3.72 | 3.58 | 2.07 | England | Premier League |
| 2 | 10 May 2021 | 19:00 | Fulham - Burnley | 0:2 | 2.24 | 3.44 | 3.38 | England | Premier League |
| 3 | 09 May 2021 | 18:00 | Arsenal - West Brom | 3:1 | 1.5 | 4.53 | 6.76 | England | Premier League |
| 4 | 09 May 2021 | 15:30 | West Ham - Everton | 0:1 | 2.15 | 3.56 | 3.48 | England | Premier League |
我正在尝试为同一网站的另一个url获取类似的输出:
urls = {
"https://www.oddsportal.com/matches/soccer/"
}
我怎样才能做到
p、 我对网络垃圾很陌生,我非常感谢你的帮助
逐字逐句,因为这样需要更多的解释:
这段代码是我继承的,我想为网页上列出了多个比赛的URL提取一个类似的数据框。这是很多你要求别人为你编写的代码。但是,如果只添加到
url={”https://www.oddsportal.com/soccer/england/premier-league/results/" "https://www.oddsportal.com/matches/soccer/“}