简体   繁体   English

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

[英]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.

相关问题 当我尝试在 windows 上运行代码时,Pyaudio 不起作用 - Pyaudio doesn't work when I try to run the code on windows 代码在 Jupiter notebook 中工作,但在终端中不能作为 .py 文件工作 - Code works in Jupiter notebook, but doesn't work in terminal as a .py file 当我尝试在Django urls.py文件中编写内容时,它在浏览器中未显示 - When I try to write something in Django urls.py file, it doesn't show this in the browser 当我尝试运行SendKeys.py时,Python无法识别_sendkeys.c文件 - Python isn't recognizing _sendkeys.c file when I try to run SendKeys.py 当我尝试运行一个py文件时出现Python错误 - Python error when I try to run one py file 当我双击 .py 文件时 GUI 会打开,但当我在 pycharm 中运行它时不会打开 - GUI opens when i double cklick the .py file, but doesn't when i run it in pycharm 当我尝试在批处理文件中使用python脚本写入文件然后运行它时。 它不会写入文件 - When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file 我的exe 从 pyinstaller 创建的文件不像它是 .py 时那样工作(Pyexcel 问题) - My exe. file created from pyinstaller doesn't work like it does when it was .py (Pyexcel problem) 我尝试使用exec_run执行到容器中,但不起作用 - I try to execute into container with exec_run but doesn't work 双击时,Python Tkinter - .py文件不起作用 - Python Tkinter - .py file doesn't work when double clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM