Reputation: 217
I am unable to scrape data from website, that uses CloudFlare. I always get urllib.error.HTTPError: HTTP Error 503: Service Temporarily Unavailable Can you show me the way to pass the CloudFlare protection?
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
#Website url was changed to ####, because it is secret
url = '#######'
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = Request(url,headers=hdr)
page = urlopen(req)
soup = BeautifulSoup(page, "lxml")
print(soup)
Can you explain me every step, please
Upvotes: 2
Views: 9679
Reputation: 21643
Sometimes requests will work even when urllib does not.
import requests
import bs4
url = ... a secret
page = requests.get(url).content
soup = bs4.BeautifulSoup(page, 'lxml')
If this does not work then I would try selenium. There are numerous examples of how to use it here on StackOverflow. I can't offer suggestions because I don't have the url you want to work with.
Upvotes: 3