pychan เป็นไคลเอนต์ Python สำหรับการโต้ตอบกับ 4chan 4chan ไม่มี API อย่างเป็นทางการและพยายามที่จะดำเนินการโดยบุคคลที่สามมีแนวโน้มที่จะอิดโรยดังนั้นไลบรารีนี้จะให้นามธรรมมากกว่าการโต้ตอบกับ (การขูด) 4chan โดยตรง pychan มุ่งเน้นไปที่วัตถุและการใช้งานนั้นขี้เกียจในกรณีที่สมเหตุสมผล (ใช้เครื่องกำเนิด Python) เพื่อเพิ่มประสิทธิภาพและลดการปิดกั้น I/O ที่ไม่จำเป็น
หากคุณติดตั้ง Python> = 3.10 และ <4.0 สามารถติดตั้ง pychan ได้จาก PYPI โดยใช้บางอย่างเช่น
pip install pychan ปฏิสัมพันธ์ 4chan ทั้งหมดถูกควบคุมตัวภายในโดยการนอนหลับด้ายที่ดำเนินการ หากคุณดำเนินการ pychan ในแบบมัลติเธรดคุณจะไม่ได้รับประโยชน์จากการควบคุมปริมาณนี้ pychan ไม่รับผิดชอบต่อผลของการร้องขอ HTTP ที่มากเกินไปในกรณีดังกล่าว
from pychan import FourChan , LogLevel , PychanLogger
# With all defaults (logging disabled, all exceptions raised)
fourchan = FourChan ()
# Tell pychan to gracefully ignore HTTP exceptions, if any, within its internal logic
fourchan = FourChan ( raise_http_exceptions = False )
# Tell pychan to gracefully ignore parsing exceptions, if any, within its internal logic
fourchan = FourChan ( raise_parsing_exceptions = False )
# Configure logging explicitly
logger = PychanLogger ( LogLevel . INFO )
fourchan = FourChan ( logger = logger )
# Use all of the above settings at once
logger = PychanLogger ( LogLevel . INFO )
fourchan = FourChan ( logger = logger , raise_http_exceptions = True , raise_parsing_exceptions = True ) ส่วนที่เหลือของตัวอย่างใน README นี้สมมติว่าคุณได้สร้างอินสแตนซ์ของคลาส FourChan ดังที่แสดงไว้ด้านบนแล้ว
ฟังก์ชั่นนี้จะดึงบอร์ดจาก 4chan แบบไดนามิกในเวลาโทร
หมายเหตุ: บอร์ดที่เข้ากันไม่ได้กับ
pychanจะไม่ถูกส่งคืนในรายการนี้
boards = fourchan . get_boards ()
# Sample return value:
# ['a', 'b', 'c', 'd', 'e', 'g', 'gif', 'h', 'hr', 'k', 'm', 'o', 'p', 'r', 's', 't', 'u', 'v', 'vg', 'vm', 'vmg', 'vr', 'vrpg', 'vst', 'w', 'wg', 'i', 'ic', 'r9k', 's4s', 'vip', 'qa', 'cm', 'hm', 'lgbt', 'y', '3', 'aco', 'adv', 'an', 'bant', 'biz', 'cgl', 'ck', 'co', 'diy', 'fa', 'fit', 'gd', 'hc', 'his', 'int', 'jp', 'lit', 'mlp', 'mu', 'n', 'news', 'out', 'po', 'pol', 'pw', 'qst', 'sci', 'soc', 'sp', 'tg', 'toy', 'trv', 'tv', 'vp', 'vt', 'wsg', 'wsr', 'x', 'xs'] # Iterate over all threads in /b/
for thread in fourchan . get_threads ( "b" ):
# Do stuff with the thread
print ( thread . title )
# You can also iterate over all the posts in the thread
for post in fourchan . get_posts ( thread ):
# Do stuff with the post - refer to the model documentation in pychan's README for details
print ( post . text )หมายเหตุ: บอร์ดบางตัวไม่มีการเก็บถาวร (เช่น
/b/) บอร์ดดังกล่าวจะส่งคืนรายการที่ว่างเปล่าหรือเพิ่มข้อยกเว้นขึ้นอยู่กับวิธีที่คุณกำหนดค่าอินสแตนซ์FourChanของคุณ
เธรดที่ส่งคืนโดยฟังก์ชั่นนี้จะมีฟิลด์ title ที่มีข้อความที่แสดงในอินเทอร์เฟซของ 4chan ภายใต้ส่วนหัวคอลัมน์ "ข้อความที่ตัดตอนมา" ข้อความนี้อาจเป็นชื่อจริงของเธรดหรือดูตัวอย่างของข้อความโพสต์ต้นฉบับ การผ่านเธรดใด ๆ ที่ส่งคืนโดยวิธีนี้ไปยังวิธี get_posts() จะแก้ไขฟิลด์ title โดยอัตโนมัติ (ถ้าจำเป็น) บนเธรดที่ติดอยู่กับโพสต์ที่ส่งคืน ดูโพสต์ Fetch สำหรับเธรดเฉพาะสำหรับรายละเอียดเพิ่มเติม
ในทางเทคนิคแล้ว
pychanสามารถระบุพฤติกรรมtitleที่อธิบายไว้ข้างต้นโดยการออกคำขอ HTTP เพิ่มเติมสำหรับแต่ละเธรดเพื่อรับชื่อจริง แต่ในจิตวิญญาณของการทำคำขอ HTTP จำนวนน้อยที่สุดpychanใช้ข้อความที่ตัดตอนมาโดยตรง
for thread in fourchan . get_archived_threads ( "pol" ):
# Do stuff with the thread
print ( thread . title )
# You can also iterate over all the posts in the thread
for post in fourchan . get_posts ( thread ):
# Do stuff with the post - refer to the model documentation in pychan's README for details
print ( post . text )การดำเนินการค้นหากับ 4chan นั้นยุ่งยากมากกว่าการเข้าถึงข้อมูลที่เหลือของ 4chan นี่เป็นเพราะ 4chan มีไฟร์วอลล์ CloudFlare อยู่ด้านหน้า REST API ดังนั้นวิธีเดียวที่จะได้รับข้อมูลกลับมาจากการค้นหาคือการให้ข้อมูลคำขอ HTTP ที่จำเป็นในการข้ามการตรวจสอบต่อต้านบอทของ Cloudflare ในที่สุดจำนวนนี้จะผ่านส่วนหัวบางส่วนพร้อมกับคำขอ HTTP แต่ความท้าทายมาจากการได้รับส่วนหัวดังกล่าว
ขณะนี้อยู่นอกเหนือขอบเขตของ pychan ที่จะสร้างส่วนหัวเหล่านี้ให้คุณดังนั้นหากคุณต้องการหลีกเลี่ยงการป้องกันของ CloudFlare โดยอัตโนมัติคุณอาจต้องการดูการใช้โครงการเช่นหนึ่งในรายการต่อไปนี้ (รายการนี้เป็นตัวอักษรและไม่ครบถ้วนสมบูรณ์):
วิธีด้วยตนเองในการรับค่าเหล่านี้คือการค้นหา 4chan โดยใช้เว็บเบราว์เซอร์และใช้ประโยชน์จากเครื่องมือนักพัฒนาของเบราว์เซอร์ในการติดตามคำขอเครือข่ายที่ทำระหว่างการค้นหา คำขอที่มีค่า CloudFlare จะถูกสร้างขึ้นเพื่อ https://find.4chan.org/api พร้อมพารามิเตอร์การสืบค้นบางอย่าง เมื่อคุณพบคำขอนี้แล้วให้คัดลอกค่า User-Agent และ Cookie ที่ส่งในคำขอของคุณแล้วส่งผ่านไปยังวิธี search() ของ pychan โปรดทราบว่าคุกกี้ CloudFlare มีการหมดอายุดังนั้นการแก้ปัญหาด้วยตนเองนี้จะส่งคืนผลลัพธ์จนกว่า CloudFlare จะทำให้คุกกี้ของคุณเป็นโมฆะ หลังจากนั้นคุณจะต้องได้รับค่าใหม่
หมายเหตุ: เธรดที่ปิด/ติด/เก็บถาวรจะไม่ถูกส่งคืนในผลการค้นหา
# This "threads" variable will contain a Python Generator (not a list) in order to facilitate laziness
threads = fourchan . search (
board = "b" ,
text = "ylyl" ,
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" ,
cloudflare_cookies = {
"cf_clearance" : "bm2RICpcDeR4cXoC2nfI_cnZcbAkN4UYpN6c1zzeb8g-1440859602-0-160"
}
)
for thread in threads :
# The thread object is the same class as the one returned by get_threads()
for post in fourchan . get_posts ( thread ):
# Do stuff with the post - refer to the model documentation in pychan's README for details
print ( post . text ) from pychan . models import Thread
# Instantiate a Thread instance with which to query for posts
thread = Thread ( "int" , 168484869 )
# Note: the thread contained within the returned posts will have all applicable metadata (such as
# title and sticky status), regardless of whether you provided such data above - pychan will
# "auto-discover" all metadata and include it in the post models' copy of the thread
posts = fourchan . get_posts ( thread )ตารางต่อไปนี้สรุปข้อมูลทุกชนิดที่มีอยู่ในรุ่นต่างๆที่ใช้โดยไลบรารีนี้
โปรดทราบว่าคลาสโมเดลทั้งหมดใน pychan ใช้วิธีการต่อไปนี้:
__repr____str____hash____eq____iter__ - สิ่งนี้ถูกนำไปใช้เพื่อให้โมเดลอาจถูกส่งผ่านไปยังฟังก์ชัน tuple() ของ Python__copy____deepcopy__ ตารางด้านล่างสอดคล้องกับ pychan.models.Thread คลาสเธรด
| สนาม | พิมพ์ | ค่าตัวอย่าง (s) |
|---|---|---|
thread.board | str | "b" , "int" |
thread.number | int | 882774935 , 168484869 |
thread.title | Optional[str] | None "YLYL thread" |
thread.is_stickied | bool | True False |
thread.is_closed | bool | True False |
thread.is_archived | bool | True False |
thread.url | str | "https://boards.4chan.org/a/thread/251097344" |
ตารางด้านล่างสอดคล้องกับคลาส pychan.models.Post
| สนาม | พิมพ์ | ค่าตัวอย่าง (s) |
|---|---|---|
post.thread | Thread | pychan.models.Thread |
post.number | int | 882774935 , 882774974 |
post.timestamp | Datetime.datetime | Datetime.datetime |
post.poster | Poster | pychan.models.Poster |
post.text | str | ">be men>be boredn>write pychann>somehow it works" |
post.is_original_post | bool | True False |
post.file | Optional[File] | None pychan.models.File |
post.replies | list[Post] | [] , [pychan.models.Post, pychan.models.Post] |
post.url | str | "https://boards.4chan.org/a/thread/251097344#p251097419" |
ฟิลด์ replies ที่แสดงด้านบนเป็นคุณสมบัติความสะดวกสบายที่ pychan จัดเตรียมไว้สำหรับการเข้าถึงโพสต์ทั้งหมดภายในเธรดซึ่งใช้ตัวดำเนินการ >> เพื่อ "ตอบกลับ" ไปยังโพสต์ปัจจุบัน อย่างไรก็ตามไม่จำเป็นต้องใช้ฟิลด์ replies เพื่อเข้าถึงโพสต์ที่มีอยู่ทั้งหมดในเธรด เมื่อคุณเรียกเมธอด get_posts() คุณจะยังคงได้รับโพสต์ทั้งหมด (ตามลำดับที่โพสต์) เป็นรายการแบนเดียว
ตารางด้านล่างสอดคล้องกับ pychan.models.Poster class
| สนาม | พิมพ์ | ค่าตัวอย่าง (s) |
|---|---|---|
poster.name | str | "Anonymous" |
poster.is_moderator | bool | True False |
poster.id | Optional[str] | None "BYagKQXI" |
poster.flag | Optional[str] | None "United States" , "Canada" |
ตารางด้านล่างสอดคล้องกับคลาส pychan.models.File
| สนาม | พิมพ์ | ค่าตัวอย่าง (s) |
|---|---|---|
file.url | str | "https://i.4cdn.org/pol/1658892700380132.jpg" |
file.name | str | "wojak.jpg" , "i feel alone.jpg" |
file.size | str | "601 KB" |
file.dimensions | tuple[int, int] | (1920, 1080) , (800, 600) |
file.is_spoiler | bool | True False |
ดูการสนับสนุน. MD สำหรับข้อมูลที่มุ่งเน้นนักพัฒนา