这次作业是抓取本地网页的商品信息,最终关于find_all()还是看了答案才解决掉。
先上结果
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/Hide/PycharmProjects/PCpro_ex1/ex2.py
{'validStar': 5, 'image': 'img/pic_0000_073a9256d9624c92a05dc680fc28865f.jpg', 'price': '$24.99', 'title': 'EarPod', 'reviews': '65 reviews'}
{'validStar': 4, 'image': 'img/pic_0005_828148335519990171_c234285520ff.jpg', 'price': '$64.99', 'title': 'New Pocket', 'reviews': '12 reviews'}
{'validStar': 4, 'image': 'img/pic_0006_949802399717918904_339a16e02268.jpg', 'price': '$74.99', 'title': 'New sunglasses', 'reviews': '31 reviews'}
{'validStar': 3, 'image': 'img/pic_0008_975641865984412951_ade7a767cfc8.jpg', 'price': '$84.99', 'title': 'Art Cup', 'reviews': '6 reviews'}
{'validStar': 4, 'image': 'img/pic_0001_160243060888837960_1c3bcd26f5fe.jpg', 'price': '$94.99', 'title': 'iphone gamepad', 'reviews': '18 reviews'}
{'validStar': 4, 'image': 'img/pic_0002_556261037783915561_bf22b24b9e4e.jpg', 'price': '$214.5', 'title': 'Best Bed', 'reviews': '18 reviews'}
{'validStar': 4, 'image': 'img/pic_0011_1032030741401174813_4e43d182fce7.jpg', 'price': '$500', 'title': 'iWatch', 'reviews': '35 reviews'}
{'validStar': 4, 'image': 'img/pic_0010_1027323963916688311_09cc2d7648d9.jpg', 'price': '$15.5', 'title': 'Park tickets', 'reviews': '8 reviews'}
Process finished with exit code 0
代码如下:
from bs4 import BeautifulSoup
from collections import Counter
with open('/Users/Hide/PycharmProjects/PCpro_ex1/my_index.html', 'r') as wb_data:
Soup = BeautifulSoup(wb_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')
#validStars = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p')
validStars = Soup.select('div > div.ratings > p:nth-of-type(2)')
reviews = Soup.select('body > div > div > div.col-md-9 > div > div > div > div.ratings > p.pull-right')
#print(images, titles, prices, validStars, reviews, sep='\n---------------------\n')
#print(validStars)
#print(isinstance(validStars[1], tuple))
#print(validStars[1])
for title, price, validStar, review, image in zip(titles, prices, validStars, reviews, images):
data = {
'title':title.get_text(),
'price':price.get_text(),
'validStar':len(validStar.find_all("span","glyphicon glyphicon-star")),
#'validStar':validStar.strings,
'reviews':review.get_text(),
'image': image.get('src')
}
#print(validStar)
print(data)
#print(repr(validStar))
出现问题:
在Soup = BeautifulSoup(wb_data, 'lxml')这一步报错:UnicodeDecodeError
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py" --multiproc --qt-support --client 127.0.0.1 --port 61329 --file /Users/Hide/PycharmProjects/PCpro_ex1/ex2.py
warning: Debugger speedups using cython not found. Run '"/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5" "/Applications/PyCharm CE.app/Contents/helpers/pydev/setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 4412 is connecting
Connected to pydev debugger (build 163.9735.8)
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 974, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/Hide/PycharmProjects/PCpro_ex1/ex2.py", line 3, in <module>
Soup = BeautifulSoup(wb_data, 'lxml')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/bs4/__init__.py", line 191, in __init__
markup = markup.read()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 16325: ordinal not in range(128)
询问老师后得知只需要用pycharm建立一个新html文件,将.html代码原封不动拷贝即可,但是依然报错。
故开始尝试解决问题:
- 在pycharm中新建一个html文件,将python代码中文件换为该html文件,正常运行。
- 将.html代码拷贝进去,报错。字面上粗略判断是转码错误,可是小白一只完全不懂如何下手。
- 笨方法,一段代码一段代码拷贝,最后发现,都是这句惹的祸!
<p>Copyright © Your Website 2014</p>
google了一下发现,https://shrtm.nu/buKk 《版权符号"圈c"的输入方法》中说道:“网页使用:& copy;(去掉空格)”。
将代码改为:
<p>Copyright © Your Website 2014</p>
果然,世界安静了。