使用Python刮取网站并打印到文件冲突

时间:2015-05-15 12:55:46

标签: python python-2.7 web-scraping beautifulsoup python-requests

我正在尝试从网站上抓取一些数据,然后将提取的数据写入文件中。这与此问题here有关。

在答案中有这个功能:

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)

整个事情很有效。但是,只要我在文件中添加一行来书写书名,那么我就会看到并非所有的书名都显示在屏幕上。好像某处存在冲突,或者某处存在时间敏感功能。

行为奇怪的代码将是这一行:

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()

0 个答案:

没有答案