2013-05-30 109 views
1

大家好,我正在做一个python脚本,它需要从网站提取数据并将日期存储到sqlite3中。我在内容提取方面遇到了问题。这里是我做beatifulsoup从网页中提取数据python

#!/usr/bin/python 
from BeautifulSoup import BeautifulSoup 
import urllib2 
import re 

url="http://m.harveynorman.com.au/tv-audio/portable-audio/ipods" 
page=urllib2.urlopen(url) 
soup = BeautifulSoup(page.read()) 
A=soup.findAll('strong',{'class':'name fn'}) 
for B in A: 

    print = B.renderContents() 

代码和输出是这样的:

"iPod touch 16GB - White 
    iPod touch 4th Gen 32GB 
Apple iPod Shuffle 2GB 
iPod touch 16GB - Black 
iPod nano 16GB 
    iPod touch 32GB" 

,我尝试使用

print = B.renderContents()[0] 

拿到指定一个插入到sqlite3的,但输出是这样的:

i 
i 
A 
i 
i 
i 

所以我的问题是如何提取指定的一个(如:iPod touch 16GB - 白色)?

回答

0
from BeautifulSoup import BeautifulSoup 
import urllib2 
import re 

url="http://m.harveynorman.com.au/tv-audio/portable-audio/ipods" 
page=urllib2.urlopen(url) 
soup = BeautifulSoup(page.read()) 
A = soup.findAll('strong',{'class':'name fn'})[0] 
print(A.renderContents()) 

产生

iPod touch 16GB - White 

for B in A: 
    print B.renderContents()[0] 

正在打印第一字符

iPod touch 16GB - White 
iPod touch 4th Gen 32GB 
Apple iPod Shuffle 2GB 
iPod touch 16GB - Black 
iPod nano 16GB 
iPod touch 32GB 
+0

欢呼伴侣每一行的,它的工作原理 –