From 4ca41c9dd80eeb7f6be886ed7b313adf7b608ce4 Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Wed, 16 Oct 2024 19:32:37 +0300 Subject: [PATCH] Small adjustment to stackoverflow example --- docs/Examples/selectorless_stackoverflow.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/Examples/selectorless_stackoverflow.py b/docs/Examples/selectorless_stackoverflow.py index 3f4a341..f54007c 100644 --- a/docs/Examples/selectorless_stackoverflow.py +++ b/docs/Examples/selectorless_stackoverflow.py @@ -11,13 +11,15 @@ page = Adaptor(response.text, url=response.url) # First we will extract the first question title and its author based on the text content first_question_title = page.find_by_text('Run Selenium Python Script on Remote Server') first_question_author = page.find_by_text('Ryan') -# If you want you can extract other questions tags like below -first_question = first_question_title.find_ancestor( - lambda ancestor: ancestor.attrib.get('id') and 'question-summary' in ancestor.attrib.get('id') -) -rest_of_questions = first_question.find_similar() -# But since nothing to rely on to extract other titles/authors from these elements without CSS/XPath selectors due to the website nature -# We will get all the rest of the titles/authors in the page depending on the first title and the first author we got above as a starting point -for i, (title, author) in enumerate(zip(first_question_title.find_similar(), first_question_author.find_similar()), start=1): - print(i, title.text, author.text) +# because this page changes a lot +if first_question_title and first_question_author: + # If you want you can extract other questions tags like below + first_question = first_question_title.find_ancestor( + lambda ancestor: ancestor.attrib.get('id') and 'question-summary' in ancestor.attrib.get('id') + ) + rest_of_questions = first_question.find_similar() + # But since nothing to rely on to extract other titles/authors from these elements without CSS/XPath selectors due to the website nature + # We will get all the rest of the titles/authors in the page depending on the first title and the first author we got above as a starting point + for i, (title, author) in enumerate(zip(first_question_title.find_similar(), first_question_author.find_similar()), start=1): + print(i, title.text, author.text)