Guest User

LCSC Scraper

a guest
Oct 14th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.16 KB | None | 0 0
  1. #!/usr/bin/fish
  2.    
  3. # this script uses the fish shell and should dump every LCSC Part
  4. #pretty much untested...
  5. 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
  6.  
  7. #as far as I can tell, both are needed. Copy from (anonymous) lcsc visit
  8. set X_CSRF_TOKEN "X-CSRF-Token: ..."
  9. set Cookie 'Cookie: ...'
  10.  
  11. function dump
  12.   set c $argv[1] #category
  13.   set p $argv[2] #page
  14.   curl 'https://lcsc.com/api/products/search' \
  15.      -H $X_CSRF_TOKEN \
  16.      -H $Cookie \
  17.      --data-raw "current_page=$p&category=$c&in_stock=false&is_RoHS=false&show_icon=false&search_content=" \
  18.      > $c-$p.json
  19. end
  20.  
  21. for c in $cats;
  22. dump $c 1
  23. #set total (jq ".result.total" $c-1.json)
  24. set total (sed -e "s/.*total\"://;s/,.*//" $c-1.json)
  25. #set pages (math "ceil($total/25)")
  26. set pages (sed -e "s/.*total_page\"://;s/,.*//" $c-1.json)
  27.  
  28. echo "$total in category $c, pages $pages"
  29. sleep 60 # one wasted minute to get the total count...
  30. for p in (seq 0 (math "$pages/30"))
  31.  for i in (seq (math "$p*30+1") (math "($p+1)*30"))  ## will issue more requests then needed
  32.   echo dump $c $i
  33.  end
  34.  sleep 60
  35. end
  36. end
Add Comment
Please, Sign In to add comment