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

Feature Request: Adding "add_jmes" and "replace_jmes" method to ItemLoader #67

Closed
IAlwaysBeCoding opened this issue Jan 10, 2017 · 6 comments · Fixed by #68
Closed

Feature Request: Adding "add_jmes" and "replace_jmes" method to ItemLoader #67

IAlwaysBeCoding opened this issue Jan 10, 2017 · 6 comments · Fixed by #68
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@IAlwaysBeCoding
Copy link

So, currently the ItemLoader class has 6 methods for loading values:
add_xpath()
replace_xpath()
add_css()
replace_css()
add_value()
replace_value()

Could we add another 2 more methods for loading data through JmesPath selectors. Currently, I have to use the SelectJmes processor to do this. Eventually, it looks really ugly and ends up taking so much line real estate.
I did a hack where I extended the ItemLoader to include those 2 extra methods that are desperately needed.

When there is json data to parse instead of html, JmesPath selectors are the best way to go for parsing, so there should be support for JmesPath selectors in the ItemLoader class as well.

@voith
Copy link

voith commented Jan 10, 2017

I had done something similar in parsel. scrapy/parsel#27

@IAlwaysBeCoding
Copy link
Author

@voith that is really nice! You went the step further and got json out of html using xpath then using jmespath to get the stuff you want. However, my request is more for pure Json data that contains no html what so ever. I am scraping a lot of sites using the React framework and all of the data I need from these sites is in the Json. Plus, apis that consume Json is also another need to use jmespath. Just like xpath/css selectors are favor over regular expressions, jmespath are also favored over turning the json data into a python dict and access directly in loops and using key indexes. Jmespath provides a query string that can be used the same way as xpath/css selectors.

@voith
Copy link

voith commented Jan 11, 2017

@IAlwaysBeCoding I understand your requirement. The above PR can also work only with Jmespath too.
My requirement was such that I needed chain selectors. I've scraped sites which give json with some embedded html in the response. So it would be nice to have nested selectors that would solve everyones need.
But the problem is that building such nested selectors is not that easy. My PR too is not a full proof solution. Its just a POC to take the idea ahead(I suspect that there will be several bugs in my implementation). There was quite a discussion held in scrapy/parsel#25 because of the complexity.

@Gallaecio Gallaecio added the enhancement New feature or request label Jul 8, 2019
@MaxValue
Copy link

I think this feature should go to the ItemLoader repository: https://github.com/scrapy/itemloaders/

But I want it very much! The current workaround is probably defining a separate Item type for that (json) data item and then setting SelectJmes as input_processor.
That way, the item unfortunately is specific to the page.

@Gallaecio
Copy link
Member

I have this working in a project, and the path forward requires 4 changes in 3 repositories, 2 of them in Scrapy:

@Gallaecio
Copy link
Member

I’m moving this to itemloaders since we have scrapy/scrapy#5894 for the remaining Scrapy work.

@Gallaecio Gallaecio transferred this issue from scrapy/scrapy Apr 19, 2023
@Gallaecio Gallaecio added the good first issue Good for newcomers label Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants