我正在尝试使用 python 和数据库来显示一系列信息。但是,我的输出只显示最后一列。我知道我没有很清楚地表达我的意思。所以,我把我的代码和输出如下:现在输出显示:
$ python pricewatch.py
Harvey Norman Site Search
iPad 2 Wi-Fi 16GB Black
iPad 2 Wi-Fi 16GB
iPad mini Wi-Fi + Cellular 32GB
iPad mini Wi-Fi 16GB
iPad mini Wi-Fi + Cellular 64GB
iPad Wi-Fi 64GB with Retina Display
iPad Wi-Fi 32GB with Retina Display
iPad 2 Wi-Fi 16GB White
iPad 2 Wi-Fi + 3G 16GB
iPad Wi-Fi + Cellular 32GB with Retina Display
iPad mini Wi-Fi + Cellular 16GB
$357
$697
$756
$647
$395
$545
$777
$487
(8, u'iPad mini Wi-Fi + Cellular 16GB', u'Harvey Norman Site Search', u'$487')
//如你所见,它只显示我的代码的最后一个
url="http://m.harveynorman.com.au/computers/tablets-readers/ipads"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
sitename=soup.find('label',{'for':'search'})
print sitename.renderContents()
productname=soup.findAll('strong',{'class':'name fn'})
for eachproductname in productname:
print eachproductname.renderContents()
productprice=soup.findAll('span',{'class':'price'})
for eachproductprice in productprice:
print eachproductprice.renderContents().replace("<span>","").replace("</span>","")
conn =sqlite3.connect('pricewatch.db')
c = conn.cursor()
c.execute("CREATE TABLE if not exists table1 (id integer, name text, store text, price real)")
eachname = eachproductname.renderContents()
eachprice = eachproductprice.renderContents().replace("<span>","").replace("</span>","")
sitename = sitename.renderContents()
assignnumber = randint(1,30) #issue here,want to assign a series of number by the scriptself
data = [(assignnumber,eachname,sitename,eachprice),
]
c.executemany('INSERT INTO table1 VALUES (?,?,?,?)',data)
#output
for row in c.execute('select * from table1'):
print row
现在,我想从数据库中得到的输出是 (1,ipadXX,HN,$199) (2,ipad xx, NH, $200) .....
希望任何人都可以提供提示或编辑我的脚本。
问候余杭