2017-03-04 5 views
-1

スクリプトはWebページをスクラップしてから.csvファイルを生成しますが、一部の行にはcuotaa_1またはcuotaa_2の値がありません。これらの行を削除するにはどうすればよいですか? PythonでPythonで値を持たない行を削除する

from urllib.request import urlopen as uReq 
from bs4 import BeautifulSoup as soup 
my_url = 'https://www.betfair.es/sport/football' 
uClient = uReq(my_url) 
page_html = uClient.read() 
uClient.close() 
page_soup = soup(page_html, "html.parser") 
containers = page_soup.findAll("li",{"class":"com-coupon-line avb-row market-1x2 large "}) 

filename = "APUESTAS.csv" 
f = open(filename, "w") 

for container in containers: 
    partidoo = container.div.div.a["data-event"] 

    title_container = container.findAll("li",{"class":"selection sel-0 "}) 
    cuotaa_1 = title_container[0].text.strip() 

    title_container1 = container.findAll("li",{"class":"selection sel-2 "}) 
    cuotaa_2 = title_container1[0].text.strip() 

    enlace0 = container.div.div.a["href"] 
    enlace = "https://www.betfair.es" + enlace0 

答えて

0

は、空白文字列は "falsey" されているので、彼らはブール値のコンテキストではfalseと考えられています。したがって、cuotaa_1cuotaa_2の両方に、次のようなコンテナループの最後にコンテンツがある行を記述します。

if cuotaa_1 and cuotaa_2: 
    f.write("{},{}\n".format(cuotaa_1,cuotaa_2)) 
関連する問題