Web crawling domain issue - python

I'm using a small script to crawl some domain links and generate a sitemap with it.
Right now it's working, it is fairly simple.
But I need to crawl a specific domain though, and this domain for some reason doesn't let me crawl anything, it does have links on it, a sitemap.xml file as well.
I guess there must be some robots.txt or any other server side trick on it for this, assuming this scenario, what could be a workaround for crawling this?
I thought about reading the sitemap xml file and writing it somewhere, but lol, it's a bit of a strange idea.
This is the domain.
And this is the code, although this works fine for now, for other domains:
import urllib.request as urllib2
from bs4 import BeautifulSoup
myurl = "https://www.google.com/"
url = urllib2.urlopen(myurl)
soup = BeautifulSoup(url,'html.parser')
all_links = soup.find_all('a')
for link in all_links:
print(link.get('href'))
Any idea/workaround for this?
Thanks a lot

The reason why you are not being able to get anything with your script is because the site is written in React, meaning that the links are being populated with Javascript. In order to crawl such sites you will need to use a tool able to execute the embedded Javascript code. You could use something like Selenium or requests-html (from the creator of the famous requests package).

Related

BeautifulSoup request is returning an empty list from LinkedIn.com/jobs

I'm new to BeautifulSoup and web scraping so please bare with me.
I'm using Beautiful soup to pull all job post cards from LinkedIn with the title "Security Engineer". After using inspect element on https://www.linkedin.com/jobs/search/?keywords=security%20engineer on an individual job post card, I believe to have found the correct 'li' portion for the class. The code works, but it's returning an empty list '[ ]'. I don't want to use any APIs because this is an exercise for me to learn web scraping. Thank you for your help. Here's my code so far:
from bs4 import BeautifulSoup
import requests
html_text = requests.get('https://www.linkedin.com/jobs/search/?keywords=security%20engineer').text
soup = BeautifulSoup(html_text, 'lxml')
jobs = soup.find_all('li', class_ = "jobs-search-results__list-item occludable-update p0 relative ember-view")
print(jobs)
As #baduker mentioned, using plain requests won't do all the heavy lifting that browsers do.
Whenever you open a page on your browser, the browser renders the visuals, does extra network calls, and runs javascript. The first thing it does is load the initial response, which is what you're doing with requests.get('https://www.linkedin.com/jobs/search/?keywords=security%20engineer')
The page you see on your browser is because of many, many more requests:
The reason your list is empty is because the html you get back is very minimal. You can print it out to the console and compare it to the browser's.
To make things easier, instead of using requests you can use Selenium which is essentially a library for programmatically controlling a browser. Selenium will do all those requests for you like a normal browser and let you access the page-source as you were expecting it to look.
This is a good place to start, but your scraper will be slow. There are things you can do in Selenium to speed things up, like running in headless-mode
which means don't render the page graphically, but it won't be as fast as figuring out how to do it on your own with requests.
If you want to do it using requests you're going to need to do a lot of snooping through the requests, maybe using a tool like postman, and see how to simulate the necessary steps to get the data from whatever page.
For example some websites have a handshake process when logging in.
A website I've worked on goes like this:
(Step 0 really) Setup request headers because the site doesn't seem to respond unless User-Agent header is included
Fetch initial HTML, get unique key from a hidden element in a <form>
Using this key, make a POST request to the url from that form
Get a session id key from the response
Setup a another POST request that combines username, password, and sessionid. The URL was in some javascript function, but I found it using the network inspector in the devtools
So really, I work strictly with Selenium if it's too complicated and I'm only getting the data once or not so often. I'll go through the heavy stuff if I'm building a scraper for an API that others will use frequently.
Hope any of this made sense to you. Happy scraping!

Using Beautiful Soup to parse Edabit - Python

I'm trying to write code to get the amount of XP obtained by completing Edabit's challenges by parsing the individual url associated with a user on the site. Here's what I have:
from bs4 import BeautifulSoup
import requests
url = "https://edabit.com/user/xHRGAqa56TcXTLEMW"
req = requests.get(url)
soup = BeautifulSoup(req.content, 'html.parser')
div = soup.find(id="react-root")
print(div)
The find is returning a value of none but I really don't know why. I think the site was made with meteor and that may be causing a problem?
Any help much appreciated.
This happens when there is dynamic content on the website which is then loaded when the javascript is executed in the browser.
You can look at the page source of your webpage in browser to see if the tag is there or not.
Since your script is not a browser but just a program which is fetching the webpage from the website, that's why the content is not being showed in your script.
If you want that javascript to be executed in the script you can setup something like splash server.
Another way would be to check the network requests that javascript is making in your browser to load that content(which is usually an API request) and make those same requests to get the content from the API directly instead of crawling it from the browser.
Hope it helps.
there is none output means soup.find didn't match any id that you searched for. Inspect the html file once again correctly. It may work.

Python BeautifulSoup && Request to scrape search engines

I'm a little confused on how to do this. I'm not sure if this is correct but I'm trying to query a search via a url. I've tried doing this:
url = 'https://duckduckgo.com/dogs?ia=meanings'
session = requests.session()
r = session.get(url)
soup = bs(r.content, 'html.parser')
I get some html back from the response; however, when I look for all the links it comes up with nothing besides the original search url.
links = soup.find_all('a')
for link in links:
print(link)
here
When I do the search on a browser and inspect the html code, all the links exist, but for some reason are not coming back to me via my request.
Anyone have any ideas, I'm trying to build a web-scraping application and I thought this would be something really easy that I could incorporate into my terminal.
The problem is that the search results and most of the page are dynamically loaded with the help of JavaScript code being executed by the browser. requests would only download the initial static HTML page, it has no JS engine since it is not a browser.
You have basically 3 main options:
use DuckDuckGo API (Python wrapper, may be there is a better one - please recheck) - this option is preferred
load the page in a real browser through selenium and then parse the HTML which is now the same complete HTML that you see in your browser
try to explore what requests are made to load the page and mimic them in your BeautifulSoup+requests code. This is the hardest and the most fragile approach that may involve complex logic and javascript code parsing.

Server errors while web scraping https in python

I'm trying to scrape this site https://propaccess.trueautomation.com/ClientDB/Property.aspx?prop_id=17471
I can type the address directly into my url bar, and I get the results I want, but when I scrape in python, i only get the source code for a "runtime error" page.
I'm thinking it might have something to do with https because I can scrape pages in the clear like craigslist.
My code is as follows,
import urllib
import re
domain = "https://propaccess.trueautomation.com/ClientDB/Property.aspx?
prop_id=17471"
htmlfile = urllib.urlopen(domain)
htmltext = htmlfile.read()
print htmltext
I'm new to python, but not to the internet. I was assuming if I could type the url into the browswer with success, I'd be able to type the same url into python. That seems to not be the case, and I don't have a clue why.
Thanks.
Mike
Update: If I browse to said url in a browser I have never used to surf this page, I get the "runtime error" page.
I cannot access the page you linked.
It seems like you are on an authenticated session, and your python code, of course, has no idea what's going on. It, thus, will return the "permission denied" or the sort of result.
If so, you probably want to pass the session cookie when you request.
The Requests library hopefully will do what you need.
(http://docs.python-requests.org/en/latest/user/advanced/#session-objects)
Hint: when you do scraping job, use incognito mode to see a web page.
How the page looks will be exactly the same to your python environment.

beautifulsoup and mechanize to get ajax call result

hi im building a scraper using python 2.5 and beautifulsoup
but im stuble upon a problem ... part of the web page is generating
after user click on some button, whitch start an ajax request by calling specific javacsript function using proper parameters
is there a way to simulate user interaction and get this result? i come across a mechanize module but it seems to me that this is mostly used to work with forms ...
i would appreciate any links or some code samples
thanks
ok so i have figured it out ... it was quite simple after i realised that i could use combination of urllib, ulrlib2 and beautifulsoup
import urllib, urllib2
from BeautifulSoup import BeautifulSoup as bs_parse
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
res = urllib2.urlopen(req)
page = bs_parse(res.read())
No, you can't do that easily. AFAIK your options are, easiest first:
Read the AJAX javascript code yourself, as a human programmer, understand it, and then write python code to simulate the AJAX calls by hand. You can also use some capture software to capture requests/responses made live and try to reproduce them with code;
Use selenium or some other browser automation tool to fetch the page on a real web browser;
Use some python javascript runner like spidermonkey or pyv8 to run the javascript code, and hook it to your copy of the HTML dom;

Categories

Resources