**如下
from bs4 import beautifulsoup
html_path = "/users/reed/documents/dev/plan-for-combating/week1/1_2/1_2answer_of_homework/index.html"
with open(html_path, 'r') as wb_data:
soup = beautifulsoup(wb_data, "lxml")
images = soup.select("body > div > div > div.col-md-9 > div > div > div > img")
names = 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")
reviews = soup.select("body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right")
stars_block = soup.find_all("div", class_="ratings")
for name, image, price, review, star_block in zip(names, images, prices, reviews, stars_block):
stars_num = len(star_block.find_all("span", class_="glyphicon glyphicon-star"))
print(name.get_text(), "\n ", image.get('src'), "\n ", price.get_text(), "\n ", review.get_text(),
"\n ", stars_num, " stars\n")
輸出結果
process finished with exit code 0總結,學習beautifulsoup裡的find和find_all函式,非常好用,再使用find_all後獲取乙個特定區塊的html**後,可以使用for in迴圈再次進入子塊進行find_all查詢。
練習作業1
一 使用while迴圈輸入 1 2 3 4 5 6 7 8 9 1 count 1 while count 11 if count 7 print else print count count 1 二 求1 100所以自然數之和 1 x 1 2 s 0 3while x 101 4 s s x 5 ...
python學習作業筆記四
usr bin env python coding utf 8 time 2018 8 16 10 20 author 迭代器 可以迭代的資料都可以稱之為iterable物件 可迭代物件 from collections import iterable print isinstance iterab...
python學習作業筆記十一
usr bin env python coding utf 8 time 2018 8 20 17 01 author 多程序 from multiprocessing import process import os 子程序要執行的 def run proc name print run chil...