简体   繁体   中英

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. 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.

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. So either you can't collect the page, or the findAll isn't finding the divs. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM