Guest User

LCSC Scraper

a guest
Oct 14th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.11 KB | None | 0 0
  1.  
  2. 1.16 KB
  3.  
  4. #!/usr/bin/fish
  5.    
  6. # this script uses the fish shell and should dump every LCSC Part
  7. #pretty much untested...
  8. set cats 308 312 316 319 320 328 348 365 380 385 395 423 440 450 460 470 487 493 500 513 515 570 575 582 601 905 938 953 986 10991 11032
  9.  
  10. #as far as I can tell, both are needed. Copy from (anonymous) lcsc visit
  11. set X_CSRF_TOKEN "X-CSRF-Token: ..."
  12. set Cookie 'Cookie: ...'
  13.  
  14. function dump
  15.   set c $argv[1] #category
  16.   set p $argv[2] #page
  17.   curl 'https://lcsc.com/api/products/search' \
  18.      -H $X_CSRF_TOKEN \
  19.      -H $Cookie \
  20.      --data-raw "current_page=$p&category=$c&in_stock=false&is_RoHS=false&show_icon=false&search_content=" \
  21.      > $c-$p.json
  22. end
  23.  
  24. for c in $cats;
  25. dump $c 1
  26. #set total (jq ".result.total" $c-1.json)
  27. set total (sed -e "s/.*total\"://;s/,.*//" $c-1.json)
  28. #set pages (math "ceil($total/25)")
  29. set pages (sed -e "s/.*total_page\"://;s/,.*//" $c-1.json)
  30.  
  31. echo "$total in category $c, pages $pages"
  32. sleep 2
  33. for p in (seq 2 $pages)
  34.   echo dumping cat $c, page $p of $pages
  35.   dump $c $p
  36.   sleep 2  # enough to not hit the rate limit. Maybe 1 is ok.
  37. end
  38.  
Add Comment
Please, Sign In to add comment