Week 1_Practice 1.2_Crawling Item Information from One Page

Some critical information has been crawled from a website. The website is as below:


The information we need is "item title", "image", "review number", "price", and "star". The result is shown here:


The general process for the web crawling could be described as below (from the course website) :

1) The html file could be read (r) or write (w) from open() function. There are two ways: 

(1) file = open('absolute or relative file path','r');    print(file.read());    file.close()

(2) with open('absolute or relative file path','r') as file:   print(file.read())

2) A special, unique label information (i.e., css path) should be identified in the html file. The relevant commands are: inspect and copy selector.  

2) One example of the css path looks like: 

"body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)"

    Note: "nth-child" should be changed for "nth-of-type(n)" in BeautifulSoap. 

3) The information, or css path, should be incorporated in soup.select('css path') to get the result list:

"stars = soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p:nth-of-type(2)')"

The "starts" is a list. 

4) In order to get a single result from the list, we could use zip() function and for "for" "in" structure, to iterate through the "zipped" lists:

"for title,image,review,price,star in zip(titles,images,reviews,prices,stars):"

5) Use get_text(), get('src'), or get("href") functions to retrieve the desired content from the tag. 

data = {

'title': title.get_text(),               # 使用get_text()方法取出文本

'image': image.get('src'),         # 使用get 方法取出带有src的图片链接

'review': review.get_text(),

'price': price.get_text(),

'star':len(star.find_all("span",class_='glyphicon glyphicon-star'))*'★'           

                                               # 使用find_all 统计有几处是★的样式        

                                              # 由于find_all()返回的结果是列表,我们再使用len()方法去计算列表中的元素个数,也就是星星的数量

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,811评论 0 23
  • 一事一日一生的反思 反思,是对一件事的反思;是对一天的反思;是对一生的反思。 自从学会写简书写印象笔记,我每天学会...
    信儿315阅读 120评论 0 0
  • 2017.3.25 我可爱的孩子们,晚上好!刚刚还在喧闹的你们,此时已经进入梦乡,尤其是老二你,平时都要妈妈陪在身...
    来自过去的信阅读 141评论 0 0
  • 自律必精进 葛飞 中德安联人寿保险有限公司济南SSC 【日精进打卡第6天】 【知~学习】 《六项精进》大纲 诵读1...
    葛飞阅读 261评论 0 0