Python实战计划学习笔记0629

2016-06-29  本文已影响0人  个十滴水

实战计划第一天,抓了一个本地网页。

最终成果是这样的:

Paste_Image.png

我的代码:

from bs4 import BeautifulSoup
info = []
with open('E:/PycharmProjects/homework2/homework2/1_2_homework_required/index.html','r') as data:
Soup = BeautifulSoup(data,'lxml')
images = Soup.select('body > div > div > div.col-md-9 > div > div > div > img')
titles = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4 > a')
prices = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.caption > h4.pull-right')
grades = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)')
counts = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right')
#  print(images,titles,grades,prices,counts)
for title,image,price,grade,count in zip(titles,images,prices,grades,counts):
data1 = {
'title' : title.get_text(),
'image' : image.get('src'),
'price' : price.get_text(),
'grade' : len(grade.find_all("span" , class_ = "glyphicon glyphicon-star" )),
'count' : count.get_text()
}
print(data1)
info.append(data1)

总结

上一篇下一篇

猜你喜欢

热点阅读