2012-12-26 74 views
0

我用下面的代码从url中提取数据(在代码中提到)。我运行的代码,但它没有给出任何输出,也没有抛出任何错误?我是Python新手,这可能是一个愚蠢的问题。有人能帮我吗?下面的代码没有得到任何输出,也没有任何错误?

import csv 
import urllib2 
import sys 
import time 
from bs4 import BeautifulSoup 
page = urllib2.urlopen('http://www.t-mobile.de/smartphones/0,22727,23392-_3-0--0-all-,00.html').read() 
soup = BeautifulSoup(page) 
soup.prettify() 
with open('TMO_DE_2012-12-26.csv', 'wb') as csvfile: 
    spamwriter = csv.writer(csvfile, delimiter=',') 
    spamwriter.writerow(["Date","Month","Day of Week","Device Name","Price"]) 
    items = soup.findAll('div', {"class": "top"},text=True) 
    prices = soup.findAll('strong', {"class": "preis-block"}) 
    for item, price in zip(items, prices): 
     textcontent = u' '.join(price.stripped_strings) 
     print unicode(item.string).encode('utf8').strip() 
     if textcontent:    
      spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A") ,unicode(item.string).encode('utf8').strip(),textcontent]) 
+0

顺便说一句,'soup.prettify()'是意味着输出可读的HTML,但不使用该输出。正如所使用的,该行不做任何事情。 –

+0

不,我没有创建任何重复的帐户,user1915050是我的朋友,他正在同一个项目上与我一起工作。 –

+0

Okidoki;编码风格有点熟悉。 :-) –

回答

0

没有<div class="top">元素与网页上的文字,所以items是一个空列表。取下text=True过滤器:

items = soup.findAll('div', {"class": "top"}) 

,并从中提取所有文本:

item_text = u' '.join(item.stripped_strings) 
if textcontent and item_text:    
    spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A") , item_text, textcontent]) 

,或者集成到现有的代码:

with open('TMO_DE_2012-12-26.csv', 'wb') as csvfile: 
    spamwriter = csv.writer(csvfile, delimiter=',') 
    spamwriter.writerow(["Date","Month","Day of Week","Device Name","Price"]) 
    items = soup.findAll('div', {"class": "top"}) 
    prices = soup.findAll('strong', {"class": "preis-block"}) 
    for item, price in zip(items, prices): 
     textcontent = u' '.join(price.stripped_strings) 
     item_text = u' '.join(item.stripped_strings) 
     if item_text and textcontent:    
      spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A"), item_text.encode('utf8'),textcontent.encode('utf8')]) 
+0

感谢您的回答。在进行更改后我运行了代码,但“项目”没有显示所有行! –

+0

@ChandanKumar:我事先测试了你的URL代码。你确定你做出了正确的改变吗? –

+0

再次感谢!有效!! –

相关问题