I do not understand how to make the correction - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: I do not understand how to make the correction (/thread-32337.html) |
I do not understand how to make the correction - Led_Zeppelin - Feb-03-2021 #!/usr/bin/env python # coding: utf-8 # In[ ]: import bs4 from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup from urllib.request import urlopen as uReq #Web Client # In[ ]: page_url = 'https://www.newegg.com/p/pl?d=graphics+cards' uReq(page_url) # In[ ]: # opens the connection and downloads html page from url uClient = uReq(page_url) # In[ ]: # parses html into a soup data structure to traverse html # as if it were a json type. page_soup = soup(uClient.read(), 'html.parser') uClient.close() # In[ ]: # finds each product from the store page containers = page_soup.find("div", {"class": "item-container"}) # In[ ]: # name the output file to write to local disk out_filename = "graphics_cards.csv" # header of csv file to be written headers = "brand,product_name,shipping \n" # In[ ]: #opens file and writes headers f = open(out_filename, "w") f.write(headers) # In[ ]: # loops over each product and grabs attributes about # each product for container in containers: # Finds all link tags "a" from within the first div. make_rating_sp = containers.div.select("a") # In[ ]: # Grabs the title from the title attribute # the does proper casing using .title() brand = make_rating_sp[0].img["title"].title() # In[ ]: # Grabs the text with the second "(a)" tag from within # a list of queries product_name = container.div.select("a")[2].text # In[ ]: # Grabs the product shipping information by searching # all lists wi th the class "price-ship". # Then cleans th text of white space with strip() # Cleans the strip of "Shipping $" if it exists to just get number shipping = container.findAll('li', {"class: price]-ship"})[0]/yext.strip().replace("$", "").replace(" Shipping". "") # In[ ]: # prints to the dataset to console print('brand: " + brand "\n') print("product_name" product_name + "\n") print("shipping: " + shipping + "\n") # In[ ]: # writes the dataset to file f.write(brand + ", " + product_name.replace(", ", "|") + shipping + "\n") # In[ ]: f.close() # Close the fileIf run on python 3.8 or higher gives an error. I believe that I understand the error, but I do not understand the correction. Here is the error and here is the correction suggested. In fact I will post the correction now:Quote:MINOR SUGGESTION The link for that can be found here: https://www.youtube.com/watch?v=XQgXKtPSzUI&t=1725s I do not know what to do with the snippet: divWithInfo = containers[0].find('div',"item-info") If one were to look at all of the replies, then it would that there are several suggestions; but which one will work? I cannot get the code box to work so I decided to post the error anyway and edit it later. Any help appreciated. Thanks. Respectfully, LZ RE: I do not understand how to make the correction - Led_Zeppelin - Feb-04-2021 Thank you. I forgot how to do it. I tried for about 40 min, yesterday, just guessing and I could not make it work, make the box work. I went ahead and posted my message hoping to come back and edit it later. I have posted messages on many forums before, but not for months. I simply forgot. Thanks for your help. R, LZ |