㈠任务内容:爬取本地网页的商品信息(商品名称、价钱、评分、浏览量)

(二)最终成果展示:

(三)我的代码:


(四)代码分析:
1.find_all()用法的文档
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#find-all
2.注意stars的CSS Selector的用法。[soup.select()尽量不使用完整selector]
使用beautifulsoup来定位想要的信息,我们通常得到一个完整的路径,然后把这个selector放入select去定位我们想要的数据。但我们复制回来的selector是浏览器上的selector,我们平时在浏览器上看到的都是经过js脚本的加工,所以selector也是经过加工的。而我们程序爬取到的网页并没有经过浏览器加工,所以所需的selector有可能和浏览器上的不一样。不能准确的定位我们想要的信息。
解决办法:尽量使用较短的selector去定位我们的数据,一般复制回来的selector前半部分可以不要,如果还是没办法准确定位,那再往前加。
# body > div:nth-child(2) > div > div.col-md-9 > div:nth-child(2) > div:nth-child(1) > div > div.ratings > p:nth-child(2) > span:nth-child(1)
#stars= Soup.select(' div > div.ratings > p:nth-of-type(2)')
3.重点难点:爬取星级评分。
网友评论