crawl.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/system/bin/sh
  2. # 全量抓取 tikwm 顶级评论(串行,含假 hasMore / 空页容错),落盘到 /sdcard/Download/_pages/
  3. PX="http://127.0.0.1:2080"
  4. VID="7552202593888603410"
  5. VURL="https://www.tiktok.com/video/$VID"
  6. DIR=/sdcard/Download/_pages
  7. LOG=/sdcard/Download/_crawl.log
  8. mkdir -p $DIR
  9. echo "START $(date)" > $LOG
  10. cursor=0
  11. emptyStreak=0
  12. pages=0
  13. got=0
  14. while [ $cursor -le 11200 ]; do
  15. f="$DIR/p_$cursor.json"
  16. pages=$((pages+1))
  17. # 已存在且有效则跳过(支持断点续传)
  18. if [ -s "$f" ] && grep -q '"code":0' "$f" && [ "$(grep -o '"video_id"' $f | wc -l)" -gt 0 ]; then
  19. n=$(grep -o '"video_id"' $f | wc -l)
  20. got=$((got+n))
  21. echo "P$pages cursor=$cursor SKIP(已有) n=$n 累计=$got" >> $LOG
  22. cursor=$((cursor+50)); emptyStreak=0; continue
  23. fi
  24. attempt=1
  25. n=0
  26. while [ $attempt -le 3 ]; do
  27. curl -s --compressed -m 40 -x "$PX" "https://www.tikwm.com/api/comment/list?url=$VURL&count=50&cursor=$cursor" -o "$f"
  28. if grep -q '"code":0' "$f" 2>/dev/null; then
  29. n=$(grep -o '"video_id"' $f | wc -l)
  30. [ "$n" -gt 0 ] && break
  31. fi
  32. rm -f "$f"
  33. attempt=$((attempt+1))
  34. sleep 2
  35. done
  36. if [ "$n" -gt 0 ]; then
  37. emptyStreak=0
  38. got=$((got+n))
  39. # 短路标志:返回条数偏少或 hasMore=false,仅记录
  40. flag=$(grep -oE '"hasMore":(true|false)' "$f" | head -1)
  41. echo "P$pages cursor=$cursor n=$n 累计=$got $flag" >> $LOG
  42. else
  43. emptyStreak=$((emptyStreak+1))
  44. echo "P$pages cursor=$cursor EMPTY (streak=$emptyStreak)" >> $LOG
  45. if [ $emptyStreak -ge 3 ]; then
  46. echo "STOP 连续3个空cursor" >> $LOG
  47. break
  48. fi
  49. fi
  50. cursor=$((cursor+50))
  51. sleep 0.4
  52. done
  53. echo "DONE cursor=$cursor pages=$pages 累计条目=$got $(date)" >> $LOG
  54. echo "DONE" > /sdcard/Download/_crawl.done