# 导入三个工具包
import requests
from bs4 import BeautifulSoup
import urllib.request
import html
def getImg():
# 解析网站
url = requests.get("https://www.buxiuse.com/?page=2") # 爬取网站url
# 获取网站数据
print(html)
# 打印输出网站数据
print(html)
getImg()
"""
获取BeautifulSoup对象
html 表示被解析的html格式的内容
html.parser表示解析用的解释器
"""
soup = BeautifulSoup(html, "html.parser")
# 获取所有img标签
girl = soup.find_all('img')
# 打印标签
print(girl)
x = 0
# 获取图片路径
for i in girl:
# 获取src路径
src = i.get('src')
print(src)
# 下载图片 利用urllib
urllib.request.urlretrieve(src, "./image/%s.jpg" % x)
x += 1
print("正在下载第%d张:" % x)
运行结果
<module 'html' from 'D:\\python\\lib\\html\\__init__.py'>
<module 'html' from 'D:\\python\\lib\\html\\__init__.py'>
Traceback (most recent call last):
File "C:\Users\王林\PycharmProjects\pythonProject\Demo.py", line 26, in <module>
soup = BeautifulSoup(html, "html.parser")
File "F:\pychrom文件\lib\site-packages\bs4\__init__.py", line 313, in __init__
elif len(markup) <= 256 and (
TypeError: object of type 'module' has no len()
Process finished with exit code 1
求告诉我这module的对象没有len()怎么处理,谢谢!