Python网络爬虫:15个高效爬虫开发技巧

对Python感兴趣,想要有更深入了解的朋友可以试试我的这份学习方法和籽料,免费自取!!

技巧一:选择合适的库

在开始编写网络爬虫之前,首先需要确定使用的库。Python中有多个用于网络爬虫的库,其中最常用的有requestsBeautifulSoupScrapy

  • requests:用于发送HTTP请求,获取网页内容。

  • BeautifulSoup:用于解析HTML文档,提取数据。

  • Scrapy:一个完整的爬虫框架,适合大规模数据抓取。

示例代码

import requests
from bs4 import BeautifulSoup

# 发送GET请求
response = requests.get('https://www.example.com')
# 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
# 提取标题
title = soup.find('title').text
print(title)

技巧二:遵守robots.txt规则

每个网站都有一个robots.txt文件,规定了哪些页面可以被爬取。尊重这些规则不仅是道德上的要求,也是法律上的义务。

如何检查

import requests

# 获取robots.txt内容
robots_url = 'https://www.example.com/robots.txt'
response = requests.get(robots_url)
print(response.text)

技巧三:设置合理的请求头

为了模拟浏览器行为,避免被网站识别为爬虫,需要设置合理的请求头。

示例代码

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}

response = requests.get('https://www.example.com', headers=headers)
print(response.status_code)

技巧四:处理JavaScript动态加载的内容

许多网站使用JavaScript动态加载内容,普通爬虫无法获取这些数据。可以使用SeleniumPyppeteer等工具来模拟浏览器行为。

示例代码(Selenium)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')  # 无界面模式
driver = webdriver.Chrome(options=options)

driver.get('https://www.example.com')
content = driver.page_source
print(content)

技巧五:使用代理IP

频繁访问同一个网站可能会导致IP被封禁。使用代理IP可以有效避免这个问题。

示例代码

proxies = {
    'http': 'http://123.45.67.89:8080',
    'https': 'https://123.45.67.89:8080'
}

response = requests.get('https://www.example.com', proxies=proxies)
print(response.status_code)

技巧六:设置合理的延时

为了避免给目标网站带来过大负担,可以设置合理的延时。常用方法是使用time.sleep()函数。

示例代码

import time

for i in range(10):
    response = requests.get('https://www.example.com/page/' + str(i))
    print(response.status_code)
    time.sleep(2)  # 每次请求后等待2秒

技巧七:使用Cookie

有些网站需要登录才能访问某些页面。可以通过设置Cookie来模拟登录状态。

示例代码

cookies = {
    'sessionid': 'abc123',
    'csrftoken': 'xyz789'
}

response = requests.get('https://www.example.com/private', cookies=cookies)
print(response.status_code)

技巧八:使用数据库存储数据

爬取的数据量较大时,建议使用数据库存储。常见的数据库有SQLite、MySQL、MongoDB等。

示例代码(SQLite)

import sqlite3

conn = sqlite3.connect('data.db')
c = conn.cursor()

c.execute('''CREATE TABLE IF NOT EXISTS articles
             (title TEXT, content TEXT)''')

# 插入数据
c.execute("INSERT INTO articles VALUES (?, ?)", ('Example Title', 'Example Content'))
conn.commit()

# 查询数据
c.execute("SELECT * FROM articles")
print(c.fetchall())

conn.close()

技巧九:使用多线程或异步提高效率

对于大型爬虫项目,可以使用多线程或多进程来提高效率。Python中常用的并发库有threadingmultiprocessingasyncio等。

示例代码(多线程)

import threading

def fetch_page(url):
    response = requests.get(url)
    print(response.status_code)

urls = ['https://www.example.com/page/' + str(i) for i in range(10)]

threads = []
for url in urls:
    thread = threading.Thread(target=fetch_page, args=(url,))
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

技巧十:使用Scrapy框架

Scrapy是一个强大的爬虫框架,支持自动处理请求、解析数据、存储结果等功能。

示例代码(Scrapy基础配置)

import scrapy

class ExampleSpider(scrapy.Spider):
    name = 'example'
    start_urls = [
        'https://www.example.com/page/1',
        'https://www.example.com/page/2',
    ]

    def parse(self, response):
        for title in response.css('h1::text'):
            yield {'title': title.get()}

        next_page = response.css('a.next::attr(href)').get()
        if next_page is not None:
            yield response.follow(next_page, self.parse)

下一篇文章将介绍更多高级技巧。

************************************************### Python网络爬虫:15个高效爬虫开发技巧

技巧十一:使用队列管理任务

在爬虫开发过程中,通常需要处理大量任务。使用队列可以有效地管理和调度这些任务。Python中的queue模块提供了队列实现。

示例代码

import queue
import threading

# 创建队列
task_queue = queue.Queue()

# 向队列中添加任务
for i in range(10):
    task_queue.put('https://www.example.com/page/' + str(i))

# 定义任务处理函数
def process_task():
    while not task_queue.empty():
        url = task_queue.get()
        response = requests.get(url)
        print(f"Processing {url} - Status Code: {response.status_code}")
        task_queue.task_done()

# 创建并启动线程
threads = []
for _ in range(5):  # 创建5个线程
    thread = threading.Thread(target=process_task)
    thread.start()
    threads.append(thread)

# 等待所有任务完成
task_queue.join()

# 等待所有线程结束
for thread in threads:
    thread.join()

技巧十二:处理验证码

一些网站会通过验证码(CAPTCHA)来防止自动化爬虫。处理验证码的方法包括OCR技术或第三方服务。

示例代码(使用OCR技术)

import requests
from PIL import Image
import pytesseract

# 下载验证码图片
captcha_url = 'https://www.example.com/captcha'
response = requests.get(captcha_url)
with open('captcha.png', 'wb') as f:
    f.write(response.content)

# 使用OCR识别验证码
image = Image.open('captcha.png')
captcha_text = pytesseract.image_to_string(image)
print(f"Captcha Text: {captcha_text}")

技巧十三:处理重定向

网站可能会进行重定向操作,导致爬虫获取不到预期的页面。可以使用requests库中的allow_redirects参数来控制是否跟随重定向。

示例代码

# 默认情况下,requests会自动处理重定向
response = requests.get('https://www.example.com/redirect', allow_redirects=True)
print(f"Final URL: {response.url}")

# 如果不希望自动处理重定向
response = requests.get('https://www.example.com/redirect', allow_redirects=False)
print(f"Status Code: {response.status_code}")

技巧十四:处理编码问题

网页内容可能存在不同的字符编码。正确的编码处理可以避免乱码问题。

示例代码

response = requests.get('https://www.example.com')
# 自动检测编码
response.encoding = response.apparent_encoding
print(response.text)

技巧十五:异常处理

在爬虫开发过程中,可能会遇到各种异常情况,如网络错误、超时等。合理的异常处理可以保证程序的稳定运行。

示例代码

import requests
from requests.exceptions import RequestException

def fetch_page(url):
    try:
        response = requests.get(url)
        response.raise_for_status()  # 检查响应状态码
        return response.text
    except RequestException as e:
        print(f"Error fetching {url}: {e}")
        return None

url = 'https://www.example.com'
html = fetch_page(url)
if html:
    print(html[:100])

实战案例:爬取新闻网站的文章列表

假设我们要爬取一个新闻网站的文章列表。该网站的结构如下:

  • 首页包含多个分类链接。

  • 每个分类页面包含多篇文章链接。

  • 每篇文章页面包含标题、作者和发布时间。

分析步骤

1. 获取首页分类链接:发送GET请求到首页,解析出各个分类的链接。2. 获取分类页面文章链接:依次访问每个分类页面,解析出每篇文章的链接。3. 获取文章详情:访问每篇文章页面,解析出标题、作者和发布时间。

示例代码

import requests
from bs4 import BeautifulSoup

# 发送GET请求获取首页
home_url = 'https://www.example-news.com'
response = requests.get(home_url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')

# 解析分类链接
category_links = [a['href'] for a in soup.select('.categories a')]
print(f"Category Links: {category_links}")

# 获取分类页面文章链接
article_links = []
for category_link in category_links:
    category_url = home_url + category_link
    response = requests.get(category_url)
    response.raise_for_status()
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 解析文章链接
    article_links.extend([a['href'] for a in soup.select('.articles a')])
    print(f"Articles from {category_link}: {article_links}")

# 获取文章详情
for article_link in article_links:
    article_url = home_url + article_link
    response = requests.get(article_url)
    response.raise_for_status()
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 解析文章标题、作者和发布时间
    title = soup.find('h1').text.strip()
    author = soup.find('span', class_='author').text.strip()
    publish_date = soup.find('span', class_='date').text.strip()
    
    print(f"Title: {title}")
    print(f"Author: {author}")
    print(f"Publish Date: {publish_date}")
    print("-" * 40)

好了,今天的分享就到这里了,我们下期见。如果本文对你有帮助,请动动你可爱的小手指点赞、收藏吧!

Python学习资料分享:

【领取方法:见文章结尾】

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

图片

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,还有环境配置的教程,给大家节省了很多时间。

三、全套PDF电子书

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

 

四、入门学习视频全套

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

图片

五、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

图片

六、互联网企业面试真题

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 这份完整版的Python全套学习资料已经整理好,朋友们如果需要可以微信扫描下方二维码免费获取【保证100%免费】。

猜你喜欢

转载自blog.csdn.net/m0_62283350/article/details/142816964