การหมุนเวียน User Agent และส่วนหัว
พัฒนาการหมุนเวียน user agent และส่วนหัว HTTP แบบไดนามิก เพื่อเลียนแบบการรับส่งข้อมูลจากเบราว์เซอร์ที่ถูกต้องและหลีกเลี่ยงการตรวจจับ
การหมุนเวียน User Agent และส่วนหัว เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Avoiding Detection
When you scrape websites, they often try to detect if you're a human or a bot. If they think you're a bot, they might block you!
One common way websites spot bots is by looking at your HTTP headers. These headers contain information about your request.
Understanding HTTP Headers
Every time your browser (or a script) makes a request to a website, it sends HTTP headers. Think of them as metadata attached to your request.
- User-Agent: Identifies your browser/OS.
- Accept: What content types you prefer.
- Referer: The previous page you were on.
- Accept-Language: Your preferred language.
Your Digital Fingerprint
The User-Agent header is super important. It tells the web server what kind of client is making the request.
For example, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" identifies a Chrome browser on Windows.
Bots often use a generic User-Agent, or none at all, which is a big red flag!
Spotting Suspicious Patterns
If a website sees many requests from the same IP address, all using the exact same, generic User-Agent, it's easy to tell it's a bot.
Web servers can also analyze other headers. A real browser sends a rich set of headers, while a simple scraping script might send very few.
Building a User-Agent List
To mimic real browsers, you need a collection of diverse User-Agent strings. You can find these online!
- Search for "list of user agents".
- Extract them from browser requests.
- Use libraries that maintain such lists.
Aim for a mix of different browsers (Chrome, Firefox, Safari) and operating systems.
Your First Custom Header
In Python, using the requests library, you can easily set custom headers for your requests. Let's start with a single User-Agent.
Try running this example:
import requests
url = "https://httpbin.org/headers"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
}
response = requests.get(url, headers=headers)
print(response.json()['headers']['User-Agent'])Dynamic User-Agent Selection
To make your requests appear more natural, you should rotate your User-Agent. This means picking a different one for each request (or after a few requests).
We can use Python's random module to select a User-Agent from a list.
Try running this example:
import requests
import random
user_agents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
]
url = "https://httpbin.org/headers"
random_ua = random.choice(user_agents)
headers = {"User-Agent": random_ua}
response = requests.get(url, headers=headers)
print(f"Used UA: {random_ua}")
print(response.json()['headers']['User-Agent'])Beyond User-Agent
While User-Agent is crucial, real browsers send many other headers. Including a few more can make your bot look even more legitimate.
- Accept-Language: e.g.,
en-US,en;q=0.9 - Accept-Encoding: e.g.,
gzip, deflate, br - Connection: e.g.,
keep-alive
You can rotate these too, or pick a consistent set that matches your chosen User-Agent's browser type.
A More Complete Header Set
Let's combine random User-Agent selection with a few other common headers to create a more robust request.
This makes your scraping bot harder to distinguish from a regular browser.
Try running this example:
import requests
import random
user_agents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1"
]
url = "https://httpbin.org/headers"
random_ua = random.choice(user_agents)
headers = {
"User-Agent": random_ua,
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
}
response = requests.get(url, headers=headers)
print(f"Used UA: {random_ua}")
print("Received headers:")
for key, value in response.json()['headers'].items():
print(f" {key}: {value}")Header Rotation Check
You've learned how User-Agents and other HTTP headers are used in web requests. Rotating these can help avoid detection.
Which of the following are good reasons to rotate User-Agent and other HTTP headers when scraping?
Recap: Blending In
Great job! You've learned how critical HTTP headers, especially the User-Agent, are for web scraping.
- Websites use headers to identify clients.
- Consistent, generic headers flag bots.
- Rotating User-Agents and adding other common headers makes your bot harder to detect.
This is a powerful technique for blending in. Next, we'll explore proxy management to hide your IP address!
เรียนรู้ Python ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การหมุนเวียน User Agent และส่วนหัว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การหมุนเวียน User Agent และส่วนหัว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การหมุนเวียน User Agent และส่วนหัว”
พัฒนาการหมุนเวียน user agent และส่วนหัว HTTP แบบไดนามิก เพื่อเลียนแบบการรับส่งข้อมูลจากเบราว์เซอร์ที่ถูกต้องและหลีกเลี่ยงการตรวจจับ คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การหมุนเวียน User Agent และส่วนหัว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม
ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การหมุนเวียน User Agent และส่วนหัว
- การจัดการพร็อกซีและการหมุนเวียน IP
- กลยุทธ์การแก้ CAPTCHA
- การหลบเลี่ยงการระบุลายนิ้วมือเบราว์เซอร์