这是我的第一个代码,用于从网站中抓取一些信息。
我有一个问题,当我尝试运行它时,控制台显示以下错误:
Traceback (most recent call last):
File "/Users/rogarui/Desktop/mantequillas_scrapper.py", line 2, in <module>
from urllib.request import urlopen as uReq
ImportError: No module named request
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u "/Users/rogarui/Desktop/mantequillas_scrapper.py"]
[dir: /Users/rogarui/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
您能帮我吗,我已经尝试将urllib更改为urllib3,但是如果执行此操作,错误将更改为:
"Traceback (most recent call last):
File "/Users/rogarui/Desktop/mantequillas_scrapper.py", line 2, in <module>
from urllib3.request import urlopen as uReq
ImportError: cannot import name urlopen
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "/Users/rogarui/Desktop/mantequillas_scrapper.py"]
[dir: /Users/rogarui/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]"
这是我的代码:
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.hsnstore.com/mantequillas-y-cremas'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div",{"clas":"col-xs-9 no-gutter list_product_right_box "})
for container in containers:
marca = container.a.text
producto = container.find("div",{"class":"col-xs-12 no-gutter product_name_link "})
nombre_producto = producto_titulo.a.text.strip()
precio_2 = container.find("div",{"class":"regular-price"})
precio_inicial = precio_2.text.strip()
precio_1 = container.find("div",{"class":"final-price"})
precio_final = precio_1.text.strip()
print("marca: " + marca)
print("nombre producto: " + nombre_producto)
print("precio inicial: " + precio_inicial)
print("precio final: " + precio_final)