Webscraping with requests returning parent webpage HTML - python

I am trying to scrape some data from a specific website using the requests and Beautiful Soup libraries. Unfortunately, I am not receiving the HTML for that page, but for the parent page https://salesweb.civilview.com. Thank you for your help!
import requests
from bs4 import BeautifulSoup
example="https://salesweb.civilview.com/Sales/SaleDetails?PropertyId=473016965"
exampleGet=requests.get(example)
exampleGetText=exampleGet.text
soup = BeautifulSoup(exampleGetText,"lxml")
soup

You need to feed a cookie to the request:
import requests
from bs4 import BeautifulSoup
cookie = {'ASP.NET_SessionId': 'rk2b0dxast1eyu5jvxezltgh'}
example="https://salesweb.civilview.com/Sales/SaleDetails?PropertyId=473016964"
exampleGet=requests.get(example, cookies=cookie)
exampleGetText=exampleGet.text
soup = BeautifulSoup(exampleGetText,"lxml")
soup.title
<title>Sales Listing Detail</title>
That particular cookie may not work for you, so you'll need manually navigate to that page one time, then go into the developer (web inspector) tools in your browser, and lookup the cookie under "Headers" in the network tab. My cookie looked like 'ASP.NET_SessionId=rk2b0dxast1eyu5jvxezltgh'.
The cookie should be valid for other property pages as well.

Related

Beautiful Soup Link Scraping [duplicate]

I am testing using the requests module to get the content of a webpage. But when I look at the content I see that it does not get the full content of the page.
Here is my code:
import requests
from bs4 import BeautifulSoup
url = "https://shop.nordstrom.com/c/womens-dresses-shop?origin=topnav&cm_sp=Top%20Navigation-_-Women-_-Dresses&offset=11&page=3&top=72"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
print(soup.prettify())
Also on the chrome web-browser if I look at the page source I do not see the full content.
Is there a way to get the full content of the example page that I have provided?
The page is rendered with JavaScript making more requests to fetch additional data. You can fetch the complete page with selenium.
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()
url = "https://shop.nordstrom.com/c/womens-dresses-shop?origin=topnav&cm_sp=Top%20Navigation-_-Women-_-Dresses&offset=11&page=3&top=72"
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
driver.quit()
print(soup.prettify())
For other solutions see my answer to Scraping Google Finance (BeautifulSoup)
Request is different from getting page source or visual elements of the web page, also viewing source from web page doesn't give you full access to everything that is on the web page including database requests and other back-end stuff. Either your question is not clear enough or you've misinterpreted how web browsing works.

I try to parse internal network webpage using by beautifulsoup library but didn't same like html

I'd like to make an auto login program in internal network website.
So, I try to parse that site using requests and Beautifulsoup library.
It works...and I get some html alot shorter than that site's html.
what's the problem? maybe security issue?..
pleas help me.
import requests
from bs4 import BeautifulSoup as bs
page = requests.get("http://test.com")
soup = bs(page.text, "html.parse")
print(soup) # I get some html alot shorter than that site's html

How can I view web page content that is generated using angular JS?

I have an app that uses requests to analyze and act on web page text. But it does not seem to work on this page that is likely built with angular: https://bio.tools/bowtie, in that the source HTML is different than the actual content. I am trying to collect the DOI that is referenced on the page (10.1186/gb-2009-10-3-r25), but when requests picks up the HTML source the DOI is not there.
I've heard that Google is able to parse pages that are generated using javascript. How do they do it? Any tips on viewing the DOI information with python?
You probably need an engine which runs the javascript of the http response for you (like an internet browser does). You can use selenium for this and then parsing the html it returns with beautifulsoup.
Example:
from selenium import webdriver
from bs4 import BeautifulSoup
url = "https://bio.tools/bowtie"
path = "path/to/chrome/webdriver"
browser = webdriver.Chrome(path) # Can also be Firefox, etc.
browser.get(url)
html = browser.page_source
soup = BeautifulSoup(html, 'html.parser')
...

Not able to download complete source code of a web page

I am trying to scrape this web-page using python requests library.
But I am not able to download complete html source code. When I use my web-browser to inspect elements, it gives complete html, which I believe can be used for scraping, but when I access this url using python requests library, those html tags which have data are simply disappeared and I am not able to scrape data from those. Here is my sample code :
import requests
from bs4 import BeautifulSoup as BS
import urllib
import http.client
url = 'https://www.udemy.com/topic/financial-analysis/?lang=en'
user_agent='my-user-agent'
request = urllib.request.Request(url,headers={'User-Agent': user_agent})
html = urllib.request.urlopen(request).read()
soup = BS(html,'html.parser')
can anybody please help me out?? Thanks
The page is likely being built by javascript, meaning the site sends over the same source you are pulling from urllib, and then the browser executes the javascript, modifying the source to render the page you are seeing
You will need to use something like selenium, which will open the page in a browser, render the JS, and then return the source e.g.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.udemy.com/topic/financial-analysis/?lang=en")
driver.page_source # or driver.execute_script("return document.body.innerHTML;")
I recommend you using the stdlib module urllib2, it will allow you to comfortably get web resources.
Example:
import urllib2
response = urllib2.urlopen("http://google.de")
page_source = response.read()
AND...
For parsing the code, have a look at BeautifulSoup.
Thanks to you both, #blakebrojan i tried your method,, but it opened a new chrome instance and display result there,, but what i want is to get source code in my code and scrape data from that code ... here is the code
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\Lenovo\\Desktop\\chrome-driver\\chromedriver.exe')
driver.get("https://www.udemy.com/topic/financial-analysis/?lang=en")
html=driver.page_source

How do I find the url of a website?

I'm currently trying to webscrape a website, and am doing so with python's urllib.request and bs4. However, this particular website has a truncated/dummy url, and so I can't put in the url and work with the html.
import urllib.request
import bs4 as bs
mylink = urllib.request.urlopen("http://www.vacationstogo.com/ticker.cfm").read()
soup = bs.BeautifulSoup(mylink, "html.parser")
NOTE:
http://www.vacationstogo.com/custom.cfm is the website where I fill in some inputs, and then when I click the search button, I get the url http://www.vacationstogo.com/ticker.cfm. Note however, that the previous URL will redirect me to some empty search page, and is not the url for the website with my search results.
Thanks.

Categories

Resources