Scraping a website using Python and printing to a file conflict

I am trying to scrape some data from a website and then write the extracted data in a file. This is related to this question here.

In the answer there is this function:

def get_books(data): soup = BeautifulSoup(data) for title in soup.select("div.zg_itemImmersion div.zg_title a"): print title.get_text(strip=True)

And the whole thing works great. But, as soon as I add a line to write the book title in a file, then I see that not all the book titles are displayed on screen. As if there is a conflict somewhere or there is a time sensitive function somewhere.

The code that will behave strangely will be alone this line:

def get_books(data): soup = BeautifulSoup(data) f = open('myFile','a') for title in soup.select("div.zg_itemImmersion div.zg_title a"): print title.get_text(strip=True) f.write(title.get_text(strip=True)) f.close()

Category:python Views:0 Time:2018-05-15

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.175 (s). 11 q(s)