I can't figure out why I get a blank output file

JLegend24
import csv
import requests
import re
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf8')

#CREATE CSV FILE

outfile = open("./output.csv", "wb")
writer = csv.writer(outfile)

#IMPORT MATCHES

import csv
with open('matches.csv', 'rb') as f:
    reader = csv.reader(f)
    matches = list(reader)

for id in matches:
    id = str(id)
    id = re.sub("[^0-9]","",id)
    url = 'http://www.virtualpronetwork.com/apps/fvpaa/matches/match_report/' + id
    print (url)
    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html)
#GET TEAMS AND SCORES

score = soup.findAll("div",{"class":"col-md-5 center"})

    team_home = score[0]
    team_home = str(team_home)
    team_home = re.search('title="(.*)" />',team_home)
    team_home = team_home.group(1)

    team_away = score[1]
    team_away = str(team_away)
    team_away = re.search('title="(.*)" />',team_away)
    team_away = team_away.group(1)

    goals_home = score[2]
    goals_home = str(goals_home)
    goals_home = re.sub('</h2></div>','',goals_home)
    goals_home = re.sub('<div class="col-md-5 center"><h2>','',goals_home)

    goals_away = score[3]
    goals_away = str(goals_away)
    goals_away = re.sub('</h2></div>','',goals_away)
    goals_away = re.sub('<div class="col-md-5 center"><h2>','',goals_away)
#GET HOME STATS

tables = soup.findChildren('table')

    stats_home = tables[0]
    list_of_rows_home = []
    for row in stats_home.findChildren('tr')[1:]:
        list_of_cells = []
        for cell in row.findChildren('td')[0]:
            text = cell.text
            list_of_cells.append(text)
        for cell in row.findChildren('td')[1]:
            text = cell.text
            list_of_cells.append(text)
        for cell in row.findChildren('td')[2:]:
            list_of_cells.append(cell)
        list_of_rows_home.append(list_of_cells) 

    for i in range(len(list_of_rows_home)):
        row = list_of_rows_home[i]
        cell = list_of_rows_home[i][2]
        cell = str(cell)
        goal = re.findall('goal',cell)
        goal = goal.count('goal')
        goal = goal / 2
        assist = re.findall('assist',cell)
        assist = assist.count('assist')
        assist = assist / 2
        motm = re.findall('motm',cell)
        motm = motm.count('motm')
        row.append(goal)
        row.append(assist)
        row.append(motm)

    for row in list_of_rows_home:
        del row[2]

    for i in range(len(list_of_rows_home)):
        row = list_of_rows_home[i]
        row.append(team_home)
        row.append(goals_home)
        row.append(team_away)
        row.append(goals_away)  

#GET AWAY STATS
stats_away = tables[1]
    list_of_rows_away = []
    for row in stats_away.findChildren('tr')[1:]:
        list_of_cells = []
        for cell in row.findChildren('td')[0]:
            text = cell.text
            list_of_cells.append(text)
        for cell in row.findChildren('td')[1]:
            text = cell.text
            list_of_cells.append(text)
        for cell in row.findChildren('td')[2:]:
            list_of_cells.append(cell)
        list_of_rows_away.append(list_of_cells)

    for i in range(len(list_of_rows_away)):
        row = list_of_rows_away[i]
        cell = list_of_rows_away[i][2]
        cell = str(cell)
        goal = re.findall('goal',cell)
        goal = goal.count('goal')
        goal = goal / 2
        assist = re.findall('assist',cell)
        assist = assist.count('assist')
        assist = assist / 2
        motm = re.findall('motm',cell)
        motm = motm.count('motm')
        row.append(goal)
        row.append(assist)
        row.append(motm)

    for row in list_of_rows_away:
        del row[2]

    for i in range(len(list_of_rows_away)):
        row = list_of_rows_away[i]
        row.append(team_away)
        row.append(goals_away)
        row.append(team_home)
        row.append(goals_home)
#COMPILE INTO ONE TABLE
list_of_rows = list_of_rows_home + list_of_rows_away
#WRITE TO CSV
writer.writerows(list_of_rows)

My input file is a basic excel file with the match id's all lined up in column one of the excel file. When it creates the output file, it's blank. I am not getting any error messages either.

l'L'l

The issue is in your regex search, so perhaps change it to:

team_home = re.search('title="(.*)"',team_home)
team_home = team_home.group(1)

Alternative:

team_home = re.search('title="(.*)"/>',team_home)
team_home = team_home.group(1)

The /> is not needed, and this essentially makes title="" not match for group(1), which in turn creates an Attribute Error, and the script stops. If you want to include /> then remove the space in your regex pattern, since that is ultimately what kills it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

My Javascript output is reversed and I can't figure out why

From Dev

I can't figure this out

From Dev

clearInterval() doesn't work and I can't figure out why

From Dev

ultisnips expand not working and I can't figure out why

From Dev

I can't figure out why my clearfix isnt working

From Dev

Can't figure out why I'm getting this NullPointerException

From Dev

this.props seems to be undefined and I can't figure out why

From Dev

I can't figure out why ArrayIndexOutOfBoundsException is thrown here

From Dev

I can't figure out why there is a syntax error in this code

From Dev

I can't figure out why my clearfix isnt working

From Dev

I can't figure out why my if statement is working

From Dev

I can't figure out why the input is not updating using MobX

From Dev

this.props seems to be undefined and I can't figure out why

From Dev

For Each loop ending early and I can't figure out why

From Dev

I can't figure out why state is undefined?

From Dev

I get a 404 error when trying to access a .jsp file under my WEB-INF directory and I can't figure out why

From Dev

I can't figure out why I'm getting an undefined reference issue when calling methods within my header file

From Dev

Can't figure out why my angular view is blank

From Dev

I can't figure these errors out

From Dev

SQL Query I can't figure out

From Dev

I can't figure out the closure for this

From Dev

I can't figure these errors out

From Dev

Syntax error that I can't figure out

From Dev

Some Thing that I Can't Figure Out

From Dev

I can't figure out this sequence - 11110000111000110010

From Dev

A result that I can't figure out

From Dev

I can't figure out this query

From Dev

ValueError + repetition I can't figure out

From Dev

Can't figure out why I can't add a foreign key constraint

Related Related

  1. 1

    My Javascript output is reversed and I can't figure out why

  2. 2

    I can't figure this out

  3. 3

    clearInterval() doesn't work and I can't figure out why

  4. 4

    ultisnips expand not working and I can't figure out why

  5. 5

    I can't figure out why my clearfix isnt working

  6. 6

    Can't figure out why I'm getting this NullPointerException

  7. 7

    this.props seems to be undefined and I can't figure out why

  8. 8

    I can't figure out why ArrayIndexOutOfBoundsException is thrown here

  9. 9

    I can't figure out why there is a syntax error in this code

  10. 10

    I can't figure out why my clearfix isnt working

  11. 11

    I can't figure out why my if statement is working

  12. 12

    I can't figure out why the input is not updating using MobX

  13. 13

    this.props seems to be undefined and I can't figure out why

  14. 14

    For Each loop ending early and I can't figure out why

  15. 15

    I can't figure out why state is undefined?

  16. 16

    I get a 404 error when trying to access a .jsp file under my WEB-INF directory and I can't figure out why

  17. 17

    I can't figure out why I'm getting an undefined reference issue when calling methods within my header file

  18. 18

    Can't figure out why my angular view is blank

  19. 19

    I can't figure these errors out

  20. 20

    SQL Query I can't figure out

  21. 21

    I can't figure out the closure for this

  22. 22

    I can't figure these errors out

  23. 23

    Syntax error that I can't figure out

  24. 24

    Some Thing that I Can't Figure Out

  25. 25

    I can't figure out this sequence - 11110000111000110010

  26. 26

    A result that I can't figure out

  27. 27

    I can't figure out this query

  28. 28

    ValueError + repetition I can't figure out

  29. 29

    Can't figure out why I can't add a foreign key constraint

HotTag

Archive