read webpage search results with Python - python

I want to be able to generate auto alerts for certain type of matches to a web search. The first step is to read the url in Python, so that I can then parse it using BeautifulSoup or other regex based methods.
For a page like the one in the example below though, the html doesn't capture the results that I'm visualizing when I open the page with a browser.
Is there a way to actually get the HTML with search results themselves?
import urllib
link = 'http://www.sas.com/jobs/USjobs/search.html'
f = urllib.urlopen(link)
myfile = f.read()
print myfile

You cannot get the data that is being generated dynamically using javascript by using traditional urllib, urllib2 or requests modules (or even mechanize for that matter). You'll have to simulate a browser environment by using selenium with chrome or Firefox or phantomjs to evaluate the javascript in the webpage.
Have a look at Selenium Binding for python

Related

Why do i get none or empty list when using find or find_all in beautifulsoap even though the tags do contain other tags and data

I am trying to make a coronavirus tracker using beautifulsoup just for some practice.
my code is,
import requests
from bs4 import BeautifulSoup
page=requests.get("https://sample.com")
soup=BeautifulSoup(page.content,'html.parser')
table=soup.find("div",class_="ZDcxi")
print(table)
In the output its showing none, but the div tag with the class ZDcxi do have content.
please help
The data, which you see in the browser, and includes the target div, is dynamic content, generated by scripts included with the page and run in the browser. If you just search for the class name in page.content, you will find it is not there.
What many people do is use selenium to open desired pages through Chrome (or another web browser), and then, after the page finishes loading and generating dynamic content, use BeautifulSoup to harvest the content from the browser, and continue processing from there.
Find out more at Requests vs Selenium Python, and also when you search selenium vs requests/

Using Python to Download CVS "Download CSV" Button

I'm trying to download the csv file using Python from this site:https://gats.pjm-eis.com/gats2/PublicReports/GATSGenerators
There's a csv button in the top right corner that I want to automatically load into a data warehouse. I've gone through a few tutorials (new to Python) and have yet to be successful. Any recommendations?
Use the library called requests, it is:
import requests
You need it to create the request to the cvs resource.
Also there's a library used for screen-scraping called bs4
import bs4
You will need both to construct what you want. Look for a course over there on web scraping with python and bs4.
Also there's a library called csv,
import csv
You can use it to easily parse the csv file once you get it.
Check this example or google it:
https://www.digitalocean.com/community/tutorials/how-to-scrape-web-pages-with-beautiful-soup-and-python-3
Here's another course on LinkedIn learning platform
https://www.linkedin.com/learning/scripting-for-testers/welcome
Selenium did the trick for me:
from selenium import webdriver
browser = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
url = 'https://gats.pjm-eis.com/gats2/PublicReports/GATSGenerators'
browser.get(url)
browser.find_element_by_xpath('//*[#id="CSV"]').click()
browser.close()

How to get section-facts-description-text in Google Maps using python?

I am trying to get the section-facts-description-text in Google Maps.
I have tried this code already:
import urllib
from bs4 import BeautifulSoup
url = "https://www.google.co.id/maps/place/Semarang,+Kota+Semarang,+Jawa+Tengah/#-7.0247703,110.3488077,12z/data=!3m1!4b1!4m5!3m4!1s0x2e708b4d3f0d024d:0x1e0432b9da5cb9f2!8m2!3d-7.0051453!4d110.4381254"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html,"html.parser")
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.get_text()
for strong_tag in soup.find_all('span',{'class':'section-facts-description-text'}):
print strong_tag.text, strong_tag.next_sibling
What's wrong with my code? Is there anything I'm missing? Is there any option to do that action with library or API in python?
urllib requests the initial loading data off of a webpage and then terminates. In the case of many complex non-static webpages, Google Maps included, that payload consists almost entirely of JavaScript scripts, which then populate the page as you know it.
So instead of downloading the DOM elements and so on that you want, you're downloading the JavaScript that populates everything instead.
In order to pull down the loaded GMaps page instead, you'll need to use a web driver that's capable of opening the page, waiting until it's loaded, and only then downloading the content. For that you should investigate selenium.

Executing a page's JavaScript at a low level with Python?

When this page is scraped with urllib2:
url = https://www.geckoboard.com/careers/
response = urllib2.urlopen(url)
content = response.read()
the following element (the link to the job) is nowhere to be found in the source (content)
Taking a look at the full source that gets rendered in a browser:
So it would appear that the FRONT-END ENGINEER element is dynamically loaded by Javascript. Is it possible to have this Javascript executed by urllib2 (or other low-level library) without involving e.g. Selenium, BeautifulSoup, or other?
The pieces of information are loaded using some ajax request. You could use firebug extension for mozilla or google chrome has it's own tool to get theese details. Just hit f12 in google chrome while opening the URL. You can find the complete details there.
There you will find a request with url https://app.recruiterbox.com/widget/13587/openings/
Information from the above url is rendered in that web page.
From what I understand, you are building something generic for multiple web-sites and don't want to go deep down in how a certain site is loaded, what requests are made under-the-hood to construct the page. In this case, a real browser is your friend - load the page in a real browser automated via selenium - then, once the page is loaded, pass the .page_source to lxml.html (from what I see this is your HTML parser of choice) for further parsing.
If you don't want a browser to show up or you don't have a display, you can go headless - PhantomJS or a regular browser on a virtual display.
Here is a sample code to get you started:
from lxml.html import fromstring
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_page_load_timeout(15)
driver.get("https://www.geckoboard.com/careers/")
# TODO: you might need a delay here
tree = fromstring(driver.page_source)
driver.close()
# TODO: parse HTML
You should also know that, there are plenty of methods to locate elements in selenium and you might not even need a separate HTML parser here.
I think you're looking for something like this: https://github.com/scrapinghub/splash

How to get the content from web browser using python?

I have a webpage :
http://kff.org/womens-health-policy/state-indicator/ultrasound-requirements/#
and I need to extract the table from this webpage.
Problem Encountered : I have been using BeautifulSoup and requests to get the url content. The problem with these methods is that I am able to get the web content even before the table is being generated.
So I get empty table
< table>
< thead>
< /thead>
< tbody>
< /tbody>
< /table>
My approach : Now I am trying to open the url in the browser using
webbrowser.open_new_tab(url) and then get the content from the browser directly . This will give the server to update the table and then i will be able to get the content from the page.
Problem : I am not sure how to fetch information from Web browser directly .
Right now i am using Mozilla on windows system.
Closest link found website Link . But it gives which sites are opened and not the content
Is there any other way to let the table load in urllib2 or beautifulsoup and requests ? or is there any way to get the loaded content directly from the webpage.
Thanks
To add to Santiclause answer, if you want to scrape java-script populated data you need something to execute it.
For that you can use selenium package and webdriver such as Firefox or PhantomJS (which is headless) to connect to the page, execute the scripts and get the data.
example for your case:
from selenium import webdriver
driver = webdriver.Firefox() # You can replace this with other web drivers
driver.get("http://kff.org/womens-health-policy/state-indicator/ultrasound-requirements/#")
source = driver.page_source # Here is your populated data.
driver.quit() # don't forget to quit the driver!
of course if you can access direct json like user Santiclause mentioned, you should do that. You can find it by checking the network tab when inspecting the element on the website, which needs some playing around.
The reason the table isn't being filled is because Python doesn't process the page it receives with urllib2 - so there's no DOM, no Javascript that runs, et cetera.
After reading through the source, it looks like the information you're looking for can be found at http://kff.org/datacenter.json?post_id=32781 in JSON format.

Categories