Created
July 6, 2017 20:49
-
-
Save colyk/7f5bf83d4c94d2bfb425817bfa2fb6b3 to your computer and use it in GitHub Desktop.
Генератор анекдотов. В основном про роботов.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import random | |
from bs4 import BeautifulSoup | |
def get_html(url): | |
r = requests.get(url) | |
return r.text | |
def get_jokes(html): | |
result_tab = [] | |
soup = BeautifulSoup(html,'lxml') | |
page = soup.find('ul',class_='item-list') | |
jokes = page.find_all('li') | |
for joke in jokes: | |
result_tab.append(joke.p.text.strip()) | |
return result_tab | |
def main(): | |
url1 = 'http://anekdoty.ru/pro-robotov/' | |
url2 = 'http://anekdoty.ru/' | |
html = get_html(url1) | |
jokes = get_jokes(html) | |
jokes.extend(get_jokes(get_html(url2))) | |
print(random.choice(jokes)) | |
input() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment