|
| 1 | +# if it get data then it works event with less headers |
| 2 | +# but when it get title `Bloomberg - Are you a robot?` |
| 3 | +# then it can get recaptcha which you may see when you open page in browser. |
| 4 | +# Sometimes it needs all headers again and it has to wait few seconds before request |
| 5 | +# but sometimes it need to resolve recaptcha. |
| 6 | + |
| 7 | +import requests |
| 8 | +from bs4 import BeautifulSoup |
| 9 | + |
| 10 | +headers = { |
| 11 | + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0', |
| 12 | + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', |
| 13 | + 'Accept-Language': 'en-US;q=0.7,en;q=0.3', |
| 14 | + 'Accept-Encoding': 'gzip, deflate, br', |
| 15 | + 'Connection': 'keep-alive', |
| 16 | + 'Pragma': 'no-cache', |
| 17 | + 'Cache-Control': 'no-cache', |
| 18 | +} |
| 19 | + |
| 20 | +s = requests.Session() |
| 21 | +s.headers.update(headers) |
| 22 | + |
| 23 | +#print(s.headers) |
| 24 | + |
| 25 | +# --- get main page to get Cookies --- |
| 26 | + |
| 27 | +#url = 'https://www.bloomberg.com' |
| 28 | +#print('url:', url) |
| 29 | +#source = s.get(url, headers=headers) |
| 30 | +#soup = BeautifulSoup(source.content, 'lxml') |
| 31 | +#print('title:', soup.find('title').text) |
| 32 | + |
| 33 | +#print(source.content) |
| 34 | + |
| 35 | +# --- get page with data --- |
| 36 | + |
| 37 | +url = 'https://www.bloomberg.com/profile/company/AAPL:US' |
| 38 | +print('url:', url) |
| 39 | +source = s.get(url, headers=headers) |
| 40 | +soup = BeautifulSoup(source.content, 'lxml') |
| 41 | +print('title:', soup.find('title').text) |
| 42 | + |
| 43 | +#print(source.content) |
| 44 | + |
| 45 | +soup = BeautifulSoup(source.content, 'lxml') |
| 46 | +company_name = soup.findAll('h1', class_= 'companyName__9bd88132') |
| 47 | +company_description = soup.findAll('div', class_ = 'description__ce057c5c') |
| 48 | + |
| 49 | +print('company_name:', company_name[0].text if company_name else company_name) |
| 50 | +print('company_description:', company_description[0].text if company_description else company_description) |
0 commit comments