Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished so_web_scraper refactoring #25

Merged
merged 1 commit into from
Oct 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions autostack/so_web_scraper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,43 +147,11 @@ def print_accepted_post(post):
to print.
'''

question = None
try:
question = post.find(
attrs={
'class',
'question'
}
).find(
attrs={
'class',
'post-text'
}
)
except AttributeError:
return
finally:
if not question:
return
question = get_post_text(post, 'question')
accepted_answer = get_post_text(post, 'accepted-answer')

accepted_answer = None
try:
accepted_answer = post.find(
attrs={
'class',
'accepted-answer'
}
).find(
attrs={
'class',
'post-text'
}
)
except AttributeError:
if question is None or accepted_answer is None:
return
finally:
if not accepted_answer:
return

print(colored('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~', 'red'))
print(colored('Question:', 'red'))
Expand All @@ -200,6 +168,27 @@ def print_accepted_post(post):
print(colored('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~', 'red'))


def get_post_text(post, html_class):
'''
TODO: Write docstring.
'''

try:
return post.find(
attrs={
'class',
html_class
}
).find(
attrs={
'class',
'post-text'
}
)
except AttributeError:
return None


def print_post_text(post_text):
'''
Prints post-text from Stack Overflow.
Expand Down