1

大家好,我正在做一个python脚本,需要从网站中提取数据并将日期存储到sqlite3中。我在内容提取中遇到问题。这是我做的代码

#!/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 - 白色)???

4

1 回答 1

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
于 2013-05-30T19:36:55.247 回答