BeautifulSoup and Amazon.co.uk - python
I am trying to parse amazon to compile a list of prices, as part of a bigger project relating to statistics. However, I am stumped. I was wondering If anyone can review my code and tell me where I went wrong?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import mechanize
from bs4 import BeautifulSoup
URL_00 = "http://www.amazon.co.uk/Call-Duty-Black-Ops-PS3/dp/B007WPF7FE/ref=sr_1_2?ie=UTF8&qid=1352117194&sr=8-2"
bro = mechanize.Browser()
resp = bro.open(URL_00)
html = resp.get_data()
soup_00 = BeautifulSoup(html)
price = soup_00.find('b', {'class':'priceLarge'})
print price #this should return at the very least the text enclosed in a tag
According to the screenshot, what I wrote above should work, shouldn't it?
Well all I get in the print out is "[]", if I change the line before last to this:
price = soup_00.find('b', {'class':'priceLarge'}).contents[0].string
or
price = soup_00.find('b', {'class':'priceLarge'}).text
I get a "noneType" error.
I am quite confused as to why this is happening. The page encoding in the URL on chrome says UTF8, to which my script is adjusted in line #2.
I have changed it to ISO (as per inner HTML of the page) but this makes zero difference, so I am positive encoding is not the issue here.
Also, don't know if this is relevant at all, but my system locale on linux being UTF-8 should not cause a problem should it?
There's no need to do this as Amazon provide an API
https://affiliate-program.amazon.co.uk/gp/advertising/api/detail/main.html
The Product Advertising API helps you advertise Amazon products using product search and look up capability, product information and features such as Customer Reviews, Similar Products, Wish Lists and New and Used listings.
More detail here: Amazon API library for Python?
I'm using the API and it so much easier and reliable then scraping the data from the webpage, even with BS. You will also get access to a list of prices for new, second hand etc and not just the "headline" price.
Related
How to download all the comments from a news article using Python?
I have to admit that I don't know much html. I am trying to extract all the comments from an article in the online news using python. I tried using python BeautifulSoup, but it seems comments are not in the html source-code, but present in the inspect-element. For instance you can check here. http://www.dailymail.co.uk/sciencetech/article-5100519/Elon-Musk-says-Tesla-Roadster-special-option.html#comments My code is here and I am struck. import urllib.request as urllib2 from bs4 import BeautifulSoup url = "http://www.dailymail.co.uk/sciencetech/article-5100519/Elon-Musk-says-Tesla-Roadster-special-option.html#comments" page = urllib2.urlopen(url) soup = BeautifulSoup(page, "html.parser") I want to do this name_box = soup.find('p', attrs={'class': 'comment-body comment-text'}) but this info is not there in the source-code. Any suggestion, how to move forward?
I have not attempted things like this, but my guess is if you want to get it directly from "page source" you'll need something like selenium to actually navigate the page since the page is dynamic. Alternatively if you're only interested in comments you may use the dailymail.co.uk's api to acquire comments. Note the items in the querystring "max=1000" "&order" etc. You may also need to use the variable "offset" along side max to find all the comments if the API has a limit on the maximum "max" value. I do not know where the API is defined, you can view it by view the network requests that your browser makes while you search the webpage. You can get comment data from http://www.dailymail.co.uk/reader-comments/p/asset/readcomments/5100519?max=1000&order=desc&rcCache=shout for that page in JSON format. It appears that every article has something like "5101863" in its url, you can use swap those numbers for each new story that you want comments about.
Thank you FredMan. I did not know about this API. It seems we need to give only the article id and we can the comments from the article. This was the solution I was looking for.
Python- Downloading a file from a webpage by clicking on a link
I've looked around the internet for a solution to this but none have really seemed applicable here. I'm writing a Python program to predict the next day's stock price using historical data. I don't need all the historical data since inception as Yahoo finance provides but only the last 60 days or so. The NASDAQ website provides just the right amount of historical data and I wanted to use that website. What I want to do is, go to a particular stock's profile on NASDAQ. For Example: (www.nasdaq.com/symbol/amd/historical) and click on the "Download this File in Excel Format" link at the very bottom. I inspected the page's HTML to see if there was an actual link I can just use with urllib to get the file but all I got was: <a id="lnkDownLoad" href="javascript:getQuotes(true);"> Download this file in Excel Format </a> No link. So my question is,how can I write a Python script that goes to a given stock's NASDAQ page, click on the Download file in excel format link and actually download the file from it. Most solutions online require you to know the url where the file is stored but in this case, I don't have access to that. So how do I go about doing this?
Using Chrome, go to View > Developer > Developer Tools In this new developer tools UI, change to the Network tab Navigate to the place where you would need to click, and click the ⃠ symbol to clear all recent activity. Click the link, and see if there was any requests made to the server If there was, click it, and see if you can reverse engineer the API of its endpoint Please be aware that this may be against the website's Terms of Service!
It appears that BeautifulSoup might be the easiest way to do this. I've made a cursory check that the results of the following script are the same as those that appear on the page. You would just have to write the results to a file, rather than print them. However, the columns are ordered differently. import requests from bs4 import BeautifulSoup URL = 'http://www.nasdaq.com/symbol/amd/historical' page = requests.get(URL).text soup = BeautifulSoup(page, 'lxml') tableDiv = soup.find_all('div', id="historicalContainer") tableRows = tableDiv[0].findAll('tr') for tableRow in tableRows[2:]: row = tuple(tableRow.getText().split()) print ('"%s",%s,%s,%s,%s,"%s"' % row) Output: "03/24/2017",14.16,14.18,13.54,13.7,"50,022,400" "03/23/2017",13.96,14.115,13.77,13.79,"44,402,540" "03/22/2017",13.7,14.145,13.55,14.1,"61,120,500" "03/21/2017",14.4,14.49,13.78,13.82,"72,373,080" "03/20/2017",13.68,14.5,13.54,14.4,"91,009,110" "03/17/2017",13.62,13.74,13.36,13.49,"224,761,700" "03/16/2017",13.79,13.88,13.65,13.65,"44,356,700" "03/15/2017",14.03,14.06,13.62,13.98,"55,070,770" "03/14/2017",14,14.15,13.6401,14.1,"52,355,490" "03/13/2017",14.475,14.68,14.18,14.28,"72,917,550" "03/10/2017",13.5,13.93,13.45,13.91,"62,426,240" "03/09/2017",13.45,13.45,13.11,13.33,"45,122,590" "03/08/2017",13.25,13.55,13.1,13.22,"71,231,410" "03/07/2017",13.07,13.37,12.79,13.05,"76,518,390" "03/06/2017",13,13.34,12.38,13.04,"117,044,000" "03/03/2017",13.55,13.58,12.79,13.03,"163,489,100" "03/02/2017",14.59,14.78,13.87,13.9,"103,970,100" "03/01/2017",15.08,15.09,14.52,14.96,"73,311,380" "02/28/2017",15.45,15.55,14.35,14.46,"141,638,700" "02/27/2017",14.27,15.35,14.27,15.2,"95,126,330" "02/24/2017",14,14.32,13.86,14.12,"46,130,900" "02/23/2017",14.2,14.45,13.82,14.32,"79,900,450" "02/22/2017",14.3,14.5,14.04,14.28,"71,394,390" "02/21/2017",13.41,14.1,13.4,14,"66,250,920" "02/17/2017",12.79,13.14,12.6,13.13,"40,831,730" "02/16/2017",13.25,13.35,12.84,12.97,"52,403,840" "02/15/2017",13.2,13.44,13.15,13.3,"33,655,580" "02/14/2017",13.43,13.49,13.19,13.26,"40,436,710" "02/13/2017",13.7,13.95,13.38,13.49,"57,231,080" "02/10/2017",13.86,13.86,13.25,13.58,"54,522,240" "02/09/2017",13.78,13.89,13.4,13.42,"72,826,820" "02/08/2017",13.21,13.75,13.08,13.56,"75,894,880" "02/07/2017",14.05,14.27,13.06,13.29,"158,507,200" "02/06/2017",12.46,13.7,12.38,13.63,"139,921,700" "02/03/2017",12.37,12.5,12.04,12.24,"59,981,710" "02/02/2017",11.98,12.66,11.95,12.28,"116,246,800" "02/01/2017",10.9,12.14,10.81,12.06,"165,784,500" "01/31/2017",10.6,10.67,10.22,10.37,"51,993,490" "01/30/2017",10.62,10.68,10.3,10.61,"37,648,430" "01/27/2017",10.6,10.73,10.52,10.67,"32,563,480" "01/26/2017",10.35,10.66,10.3,10.52,"35,779,140" "01/25/2017",10.74,10.975,10.15,10.35,"61,800,440" "01/24/2017",9.95,10.49,9.95,10.44,"43,858,900" "01/23/2017",9.68,10.06,9.68,9.91,"27,848,180" "01/20/2017",9.88,9.96,9.67,9.75,"27,936,610" "01/19/2017",9.92,10.25,9.75,9.77,"46,087,250" "01/18/2017",9.54,10.1,9.42,9.88,"51,705,580" "01/17/2017",10.17,10.23,9.78,9.82,"70,388,000" "01/13/2017",10.79,10.87,10.56,10.58,"38,344,340" "01/12/2017",10.98,11.0376,10.33,10.76,"75,178,900" "01/11/2017",11.39,11.41,11.15,11.2,"39,337,330" "01/10/2017",11.55,11.63,11.33,11.44,"29,122,540" "01/09/2017",11.37,11.64,11.31,11.49,"37,215,840" "01/06/2017",11.29,11.49,11.11,11.32,"34,437,560" "01/05/2017",11.43,11.69,11.23,11.24,"38,777,380" "01/04/2017",11.45,11.5204,11.235,11.43,"40,742,680" "01/03/2017",11.42,11.65,11.02,11.43,"55,114,820" "12/30/2016",11.7,11.78,11.25,11.34,"44,033,460" "12/29/2016",11.24,11.62,11.01,11.59,"50,180,310" "12/28/2016",12.28,12.42,11.46,11.55,"71,072,640" "12/27/2016",11.65,12.08,11.6,12.07,"44,168,130" The script escapes dates and thousands-separated numbers.
Dig a little bit deeper and find out what js function getQuotes() does. You should get a good clue from that. If it all seem too much complicated, then you can always use selenium. It is used to simulate the browser. However, it is much slower than using native network calls. You can find official documentation here.
Python Netflix Query
I am trying to get the name of TV Show (Episode/Season)/Movie from the Netflix URL. Is there a way of doing it using requests and urllib? I guess I'll need the API key and secret for that. This is what I'm trying to do. e.g. I have this URL for Z Nation. url = "https://www.netflix.com/gb/title/80008434" url_data = urlparse.urlparse(url) query = urlparse.parse_qs(url_data.query) id = query["v"][0] id should give me 80008434 netflixurl = '' r = requests.get(netflixurl) js = r.json() item = js[""] item should give me Z Nation Season 3. (Or whatever season/episode url is url) Using the pyflix2 API for 'NetflixAPIV2'. How should I go about this? Please help! EDIT: I use this for youtube. Is there a similar thing for netflix? import lxml from lxml import etree import urllib youtube = etree.HTML(urllib.urlopen("https://www.youtube.com/watch?v=L93-7vRfxNs").read()) video_title = youtube.xpath("//span[#id='eow-title']/#title") song = ''.join(video_title) Result : Daft Punk - Aerodynamic
Sadly, Netflix has discontinued the use of its public API and is not accepting any new developers. You can look into Netflix Roulette API, which is an unofficial API and lets you run queries on Netflix. You can use that API in conjunction with urllib or requests and get the results that you need. Apart from that you can use general webscraping, using BeautifulSoup and requests. But doing it this way is not recommended as it will consume immense amount of bandwidth to scrape all the directories.
There is an API that you can subscribe to developed by uNoGS. The downside is that you can have a free account but have to submit your credit card details as, if you go over 100 requests a month, you will be charged. Needless to say alarm bells rang. Therefore, I'm looking into building my own and in the very early stages. Having seen some of the replies I just thought I'd throw it out there that the robots.txt file shows that the /browse subdirectory shows as 'allowed'. Normally on websites such as this, they stipulate that they allow it for reputable search engines to be able to scrape. There is however no such clause and therefore, with the legality, as discussed so far, it appears that scraping the browse section is both legal and ethical. That being said, even there is no 'Crawl-delay' stipulated, ethically I would suggest putting one if you do succeed in getting a requests working.
so I wrote some code for this exact thing as a lot of websites gave for the USA or other regions and couldn't translate to the exact answer that worked for my Netflix. This one uses selenium but it shouldn't be hard to downloads and understand the code that I wrote. https://github.com/Eglis05/netflix-selenium You can have a look at it and report anything you don't like. :)
Python - How to scrape Tr/Td table data using 'requests & BeautifulSoup'
I'm new to programming. I'm trying out my first Web Crawler program that will help me with my job. I'm trying to build a program that will scrape tr/td table data from a web page, but am having difficulties succeeding. Here is what I have so far: import requests from bs4 import BeautifulSoup def start(url): source_code = requests.get(url).text soup = BeautifulSoup(source_code) for table_data in soup.find_all('td', {'class': 'sorting_1'}): print(table_data) start('http://www.datatables.net/') My goal is to print out each line and then export it to an excel file. Thank you, -Cire
My recommendation is that if you are new to Python, play with things via the iPython notebook (interactive prompt) to get things working first and to get a feel for things before you try writing a script or a function. On the plus side all variables will stick around and it is much easier to see what is going on. From the screen shot here, you can see immediately that the find_all function is not finding anything. An empty lists [] is being returned. By using ipython you can easily try other variants of a function on a previously defined variable. For example, the soup.find_all('td').
Looking at the source of http://www.datatables.net, I do not see any instances of the text sorting_1, so I wouldn't expect a search for all table cells of that class to return anything. Perhaps that class appeared on a different URL associated with the DataTables website, in which case you would need to use that URL in your code. It's also possible that that class only appears after certain JavaScript has been run client-side (i.e. after certain actions with the sample tables, perhaps), and not on the initially loaded page. I'd recommend starting with tags you know are on the initial page (seen by looking at the page source in your browser). For example, currently, I can see a div with class="content". So the find_all code could be changed to the following: for table_data in soup.find_all('div', {'class': 'content'}): print(table_data) And that should find something. Response to comments from OP: The precise reason why you're not finding that tag/class pairing in this case is that DataTables renders the table client-side via JavaScript, generally after the DOM has finished loading (although it depends on the page and where the DataTables init code is placed). That means the HTML associated with the base URL does not contain this content. You can see this if you curl the base URL and look at the output. However when loading it in a browser, once the JavaScript for DataTables fires, the table is rendered and the DOM is dynamically modified to add the table, including cells with the class for which you're looking.
Querying web pages with Python
I am learning web programming with Python, and one of the exercises I am working on is the following: I am writing a Python program to query the website "orbitz.com" and return the lowest airfare. The departure and arrival cities and dates are used to construct the URL. I am doing this using the urlopen command, as follows: (search_str contains the URL) from lxml.html import parse from urllib2 import urlopen parsed = parse(urlopen(search_str)) doc = parsed.getroot() links = doc.findall('.//a') the_link = (links[j].text_content()).strip() The idea is to retrieve all the links from the query results and search for strings such as "Delta", "United" etc, and read off the dollar amount next to the links. It worked successfully until today - It looks like orbitz.com has changed their output page. Now, when you enter the travel details on the orbitz.com website, there appears a page showing a wheel saying "looking up itineraries" or something to that effect. This is just a filler page and contains no real information. After a few seconds, the real results page is displayed. Unfortunately, the Python code return the links for the filler page each time, and I never obtain the real results. How can I get around this? I am a relative beginner to web programming, so any help is greatly appreciated.
This kind of things is normal in the world of crawlers. What you need to do is figure out what url it is redirecting to after the "itinerary page" and you hit that url directly from your script. Then figure out if they have changed the final search results page too, if so modify your script to accommodate those changes.