Advertisement
Guest User

Untitled

a guest
Dec 9th, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. import gspread
  2. import requests
  3. from bs4 import BeautifulSoup
  4.  
  5. HEADERS = {
  6.     'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8'
  7.               ',application/signed-exchange;v=b3;q=0.9',
  8.     'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
  9.                   'Chrome/95.0.4638.69 Safari/537.36'
  10. }
  11.  
  12. url = 'https://habr.com/ru/search/?q=python&target_type=posts&order=relevance'
  13. r = requests.get(url, headers=HEADERS, timeout=5)
  14. soup = BeautifulSoup(r.text, 'html.parser')
  15. ads = soup.find_all('div', attrs={'class': 'tm-article-snippet'})
  16.  
  17. for i in ads:
  18.     title = i.find('span', attrs={'class': 'tm-user-info tm-article-snippet__author'}).get_text(strip=True)
  19.     link = 'https://habr.com/' + i.find('a').get('href')
  20.     data = i.find('span', attrs={'class': 'tm-article-snippet__datetime-published'}).get_text(strip=True)
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement