当我尝试在终端上运行.py文件时,它不起作用



我已经创建了一个.py文件并将其保存到我的桌面。当我打开终端时,我输入cd desktop(这是显而易见的)在此之后,我输入python(文件名).py,然后当我按回车键时,它只是再次返回桌面命令行,而不运行文件。

任何想法?

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.newegg.com/global/uk-en/Desktop-Graphics-Cards/Subtegory/ID-48?nm_mc=KNC-GoogleukAdwords&cm_mmc=KNC-GoogleukAdwords-_-Sitelink-UK-_-VGA-Cards-_-Global&gclid=CjwKCAjwh5qLBhALEiwAioods8nGbLNkDI5dBNTHrJ1pprzHJDzoMXHlswOapX8G82IbGUhk1FK9gRoCczsQAvD_BwE'
#opens up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
#html parsing
page_soup = soup(page_html, "html.parser")
#grabs each product
containers = page_soup.findAll("div",{"class":"item- container"})
for container in containers:
brand = container.div.div.a.img["title"]
title_container = container.findAll("a", {"class":"item-title"})
product_name = title_container[0].text
shipping_container = container.findAll("li",    {"class":"price-ship"})
shipping = shipping_container[0].text.strip()
print("brand: " + brand)
print("product_name: " + product_name)
print("shipping: " + shipping)
#grabs each product
containers = page_soup.findAll("div",{"class":"item- container"})
print(containers)
for container in containers:
brand = container.div.div.a.img["title"]

这将显示您的容器列表为空,如:

[]

因此没有任何东西在for中循环,所以没有任何东西要打印出来。要么无法收集页面,要么findAll无法找到div。通过打印出收集页面的实际结果来开始故障排除(也许你被屏蔽了,网站不喜欢蜘蛛,等等)祝你好运!

最新更新