[英]When I try run a .py file in my terminal it doesn’t work
I've created a .py file and saved it to my desktop.我创建了一个 .py 文件并将其保存到我的桌面。 When I open terminal, I type cd desktop(which does the obvious) After this I type python (file name).py, then when I hit enter it just goes back the the desktop command line again and doesn't run the file.
当我打开终端时,我输入 cd desktop(这是显而易见的),然后我输入 python(文件名).py,然后当我按回车键时,它会再次返回桌面命令行并且不运行该文件。
Any ideas?有任何想法吗?
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"]
That will show that your containers list is empty like:这将显示您的容器列表为空,例如:
[]
thus nothing gets looped in the for, so nothing to be printed out.因此没有任何东西在 for 中循环,所以没有任何东西可以打印出来。 So either you can't collect the page, or the findAll isn't finding the divs.
所以要么你不能收集页面,要么 findAll 没有找到 div。 Start troubleshooting by printing out the actual results of collecting the page (perhaps you are blocked, the website doesn't like spiders, etc.) Good luck!
通过打印收集页面的实际结果开始故障排除(可能您被阻止,该网站不喜欢蜘蛛等)祝您好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.