Web scraping Twitter pages Using Python - python

Using my twitter developer credentials I get twitter API from news channels. Now I want to access the source of the news using URL embedded with twitter API data.
I try to get using BeautifulSoup and requests to get content of the twitter page.
But continuously I got an error 'We've detected that JavaScript is disabled in your browser. Would you like to proceed to legacy Twitter?'
I cleaned the browser and try to use every browser. But got the same response. Please help to solve this problem.
from bs4 import BeautifulSoup
import requests
url = 'https://twitter.com/i/web/status/1283691878588784642'
# get contents from url
content = requests.get(url).content
# get soup
soup = BeautifulSoup(content,'lxml')

Do you get the error 'We've detected that JavaScript is disabled in your browser. Would you like to proceed to legacy Twitter?' when running the script or when visiting it with your GUI web browser?
Have you tried getting your data through legacy?
If you get this error when running the script, there is nothig you can do like clearing browser cache, etc. The only way to get around this problem is to find another way to access the Twitter page in your python program.
From my experience I would say that the easiest way around this problem is to use gecko driver with FireFox. So Twitter gets all the features it needs.

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!

JavaScript Disabled error while web scraping twitter in Python in BeautifulSoup

I am new to this world of web scraping.
I was trying to scrape twitter with BeautifulSoup in Python.
Here's my code :
from bs4 import BeautifulSoup
import requests
request = requests.get("https://twitter.com/mybmc").text
soup = BeautifulSoup(request, 'html.parser')
print(soup.prettify())
But I am getting a large output which is not the twitter page which I am looking for but there is a error container :
Output Image
which says JavaScript is disabled in this browser. I tried changing my default browsers to Chrome, Firefox and Microsoft Edge but the out was same .
What should I do in this case?
Twitter here seem to be specifically trying to prevent scrapers of the front end, probably with the view that you should use their REST API to fetch that same data. It is not to do with your default browsers, but that requests.get will be providing a python requests user agent, which specifically doesn't support Javascript.
I'd suggest using a different page to practice on, or if it must be the twitter front page, consider using selenium perhaps with a standalone container to scrape.

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.

Webscrape a site with authentication and Sign In with Google

I need to scrape a site with authentication and I'm planning on using my google account to do so.
So far I've done:
import requests
from bs4 import BeautifulSoup
url = "https://url.com/login"
r = requests.get(url)
When I tried to follow the Sign In with Google button, I realized that there's no href link within the HTML.
Anyone can help me with this?
Thanks!
BeautifulSoup is a library that does http request directly through python, meaning that there's no browser involved. That implies that you can't scrape websites that require things like login in.
Give a look to Selenium, a library that allows you to do request through your browser.

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.

Categories

Resources