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

Improve follow/follow_all #41

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Conversation

MatthewZMSU
Copy link
Contributor

Description

In this PR I have improved follow method of PuppeteerResponse class.
Also I have added follow_all method in it.
Updated documentation.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Tested it with several local spiders.
They worked fine.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

@MatthewZMSU MatthewZMSU self-assigned this Sep 12, 2024
@MatthewZMSU MatthewZMSU added documentation Improvements or additions to documentation enhancement New feature or request labels Sep 12, 2024
Copy link
Member

@mxsnq mxsnq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have merged composite actions, please merge master and take them into account in follow methods.


class PuppeteerHtmlResponse(PuppeteerResponse):
# Probably, we should ban any PuppeteerAction in `actions` except GoTo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I believe we can even forbid any non-navigable actions in PuppeteerRequest if there is no page id.

Comment on lines 114 to 127
page_id = self.page_id # Substitution of page_id in order to create new page
self.page_id = None

yield from (
self.follow(
action,
close_page=close_page,
accumulate_meta=accumulate_meta,
**kwargs,
)
for action in actions
)

self.page_id = page_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike this pattern because it modifies the response during iteration and allows constructions like

requests_iter = response.follow_all(links)
assert response.page_id is not None
next_request = next(requests_iter, None)
assert response.page_id is None

I suggest either passing page_id=None to self.follow, or collecting following requests to list first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First option is not possible right now because in self.follow we determine page_id as page_id = None if self.puppeteer_request.close_page else self.page_id.
I modified my solution so changing self.page_id inside follow_all method is not visible to user.
Still, it modifies the response during iteration. I like the way of providing page_id=None to self.follow but currently this raises an error. Probably, it's a bug.
I suggest to solve the problem within the PR related to this comment. We will properly validate arguments not only in the service but also in the client part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants