用 Python 轻松抓取百度收录量:从原理到实战的全流程指南

**作为内容创作者,你是否经常好奇自己的文章在百度上的收录情况?掌握百度收录量不仅能直观反映内容的影响力,更是优化 SEO 策略的重要依据。今天就来教大家用 Python 实现百度收录量的自动化抓取,让数据驱动内容创作。

一、抓取原理与准备工作

1.1 百度收录量的查询逻辑

百度收录量本质上是通过特定语法在百度搜索结果中获取的统计数据。当我们在百度搜索框输入site:你的网址时,搜索结果页会显示该域名下被百度收录的网页数量。例如输入site:zhihu.com,就能看到知乎在百度的收录总量。这种查询方式基于百度的site指令,其工作原理是:百度搜索引擎会定期爬取互联网网页并建立索引,site指令相当于告诉百度 “只展示这个域名下的索引结果”,并返回收录数量。

1.2 技术栈准备

实现抓取需要以下 Python 库:

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

  • BeautifulSoup:解析 HTML 页面提取关键信息

  • re:正则表达式模块,用于处理复杂文本匹配

  • time:添加延时避免频繁请求

  • pandas:可选,用于数据存储和分析

安装命令:

  1. pip install requests beautifulsoup4 pandas

二、核心代码实现

2.1 基础抓取函数

首先实现一个基础的百度收录量抓取函数:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. import time
  5. import random
  6. def get_baidu_index(url, delay=3):
  7. """
  8. 获取指定网址的百度收录量
  9. url: 要查询的网址(如https://www.example.com)
  10. delay: 请求间隔时间(秒)
  11. """
  12. # 处理网址格式,确保以http或https开头
  13. if not url.startswith(('http://', 'https://')):
  14. url = 'https://' + url
  15. # 构造查询URL
  16. query_url = f'https://www.baidu.com/s?wd=site:{url}'
  17. # 设置请求头,模拟浏览器访问
  18. headers = {
  19. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
  20. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  21. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'
  22. }
  23. # 添加随机延时,避免被封IP
  24. time.sleep(delay + random.uniform(0, 2))
  25. try:
  26. # 发送请求
  27. response = requests.get(query_url, headers=headers, timeout=10)
  28. response.raise_for_status() # 检查请求是否成功
  29. # 解析HTML
  30. soup = BeautifulSoup(response.text, 'html.parser')
  31. html_content = soup.prettify()
  32. # 提取收录量信息
  33. index_count = extract_index_count(html_content)
  34. return {
  35. 'url': url,
  36. 'index_count': index_count,
  37. 'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
  38. 'status': 'success'
  39. }
  40. except requests.RequestException as e:
  41. print(f"请求出错: {e}")
  42. return {
  43. 'url': url,
  44. 'index_count': None,
  45. 'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
  46. 'status': 'error',
  47. 'error_msg': str(e)
  48. }
  49. except Exception as e:
  50. print(f"处理出错: {e}")
  51. return {
  52. 'url': url,
  53. 'index_count': None,
  54. 'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
  55. 'status': 'error',
  56. 'error_msg': str(e)
  57. }

2.2 解析收录量的关键函数

上面的代码中,extract_index_count函数是解析的核心:

  1. def extract_index_count(html_content):
  2. """从HTML内容中提取百度收录量"""
  3. # 百度搜索结果中收录量的常见正则模式
  4. patterns = [
  5. r'找到相关结果约(.*?)个',
  6. r'共找到(.*?)条相关结果',
  7. r'约(.*?)个网页符合'
  8. ]
  9. for pattern in patterns:
  10. match = re.search(pattern, html_content)
  11. if match:
  12. count_text = match.group(1).strip()
  13. # 处理数字格式,如"123,456"转换为123456
  14. count_text = count_text.replace(',', '')
  15. # 处理"123万"这样的格式
  16. if '万' in count_text:
  17. num = float(count_text.replace('万', '')) * 10000
  18. return int(num)
  19. # 处理"1.2亿"这样的格式
  20. elif '亿' in count_text:
  21. num = float(count_text.replace('亿', '')) * 100000000
  22. return int(num)
  23. # 纯数字格式
  24. elif count_text.isdigit():
  25. return int(count_text)
  26. else:
  27. print(f"无法解析的收录量格式: {count_text}")
  28. return None
  29. # 所有模式都未匹配
  30. print("未找到收录量信息")
  31. return None

2.3 批量查询与数据存储

实际应用中,我们通常需要批量查询多个网址的收录量并存储数据:

  1. import pandas as pd
  2. def batch_query_urls(url_list, output_file=None, delay=3):
  3. """批量查询多个网址的百度收录量"""
  4. results = []
  5. for url in url_list:
  6. print(f"正在查询: {url}")
  7. result = get_baidu_index(url, delay)
  8. results.append(result)
  9. # 显示进度
  10. print(f"查询完成: {url}, 收录量: {result.get('index_count', '未知')}")
  11. # 存储为DataFrame
  12. df = pd.DataFrame(results)
  13. # 保存到文件
  14. if output_file:
  15. if output_file.endswith('.csv'):
  16. df.to_csv(output_file, index=False, encoding='utf-8-sig')
  17. elif output_file.endswith(('.xlsx', '.xls')):
  18. df.to_excel(output_file, index=False)
  19. else:
  20. print(f"不支持的文件格式: {output_file}")
  21. return df
  22. # 使用示例
  23. if __name__ == "__main__":
  24. # 要查询的网址列表
  25. urls = [
  26. 'https://www.zhihu.com',
  27. 'https://mp.weixin.qq.com',
  28. 'https://www.toutiao.com',
  29. 'https://blog.csdn.net'
  30. ]
  31. # 批量查询并保存结果
  32. result_df = batch_query_urls(urls, 'baidu_index_results.csv', delay=5)
  33. print("查询完成,结果已保存到baidu_index_results.csv")

三、进阶优化与反爬应对

3.1 代理 IP 池的实现

为避免频繁请求导致 IP 被封,建议使用代理 IP:

  1. import random
  2. def get_proxy():
  3. """获取代理IP(实际应用中应从代理IP服务获取)"""
  4. # 这里仅为示例,实际应从代理IP服务商获取可用代理
  5. proxy_list = [
  6. 'http://127.0.0.1:8080',
  7. 'http://127.0.0.1:8081',
  8. # 添加更多代理IP
  9. ]
  10. return random.choice(proxy_list)
  11. # 在get_baidu_index函数中使用代理
  12. proxy = get_proxy()
  13. response = requests.get(query_url, headers=headers, proxies={'http': proxy, 'https': proxy}, timeout=10)

3.2 验证码处理

当百度检测到异常请求时,可能会返回验证码页面。处理方法有两种:

3.2.1 Selenium 模拟浏览器

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. def get_baidu_index_with_selenium(url):
  4. """使用Selenium模拟浏览器访问"""
  5. chrome_options = Options()
  6. # 无头模式(可选)
  7. # chrome_options.add_argument('--headless')
  8. chrome_options.add_argument('--disable-gpu')
  9. chrome_options.add_argument(f'user-agent={headers["User-Agent"]}')
  10. driver = webdriver.Chrome(options=chrome_options)
  11. try:
  12. driver.get(f'https://www.baidu.com/s?wd=site:{url}')
  13. # 等待页面加载
  14. time.sleep(5)
  15. # 如果出现验证码,需要人工处理
  16. print("请检查是否需要人工处理验证码")
  17. # 等待用户处理验证码
  18. input("处理完成后按Enter继续...")
  19. html_content = driver.page_source
  20. index_count = extract_index_count(html_content)
  21. return {
  22. 'url': url,
  23. 'index_count': index_count,
  24. 'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
  25. 'status': 'success'
  26. }
  27. finally:
  28. driver.quit()

3.2.2 验证码识别 API

对于自动化场景,可使用第三方验证码识别服务:

  1. def recognize_captcha(image_path):
  2. """调用第三方验证码识别API"""
  3. # 这里以打码平台为例,实际需根据具体API调整
  4. api_key = "你的API密钥"
  5. url = "https://api.example.com/recognize"
  6. with open(image_path, 'rb') as f:
  7. files = {'image': f}
  8. data = {'api_key': api_key}
  9. response = requests.post(url, files=files, data=data)
  10. return response.json().get('result', '')

四、实战案例:公众号收录量监控

4.1 公众号文章收录量统计

很多公众号运营者关心文章被百度收录的情况,可通过以下代码实现:

  1. def get_wechat_article_index(article_url, delay=5):
  2. """获取公众号文章的百度收录量"""
  3. # 公众号文章在百度的收录量查询
  4. return get_baidu_index(article_url, delay)
  5. # 监控多个公众号文章
  6. wechat_articles = [
  7. 'https://mp.weixin.qq.com/s/abc123', # 文章1
  8. 'https://mp.weixin.qq.com/s/def456', # 文章2
  9. # 添加更多文章链接
  10. ]
  11. # 每天定时查询
  12. import schedule
  13. def daily_monitor():
  14. """每日定时监控"""
  15. print(f"开始每日收录量监控: {time.strftime('%Y-%m-%d')}")
  16. result_df = batch_query_urls(wechat_articles, f"wechat_index_{time.strftime('%Y%m%d')}.csv")
  17. print("今日监控完成")
  18. # 设置每天9点执行监控
  19. schedule.every().day.at("09:00").do(daily_monitor)
  20. # 持续运行
  21. while True:
  22. schedule.run_pending()
  23. time.sleep(60)

4.2 数据可视化分析

使用matplotlib或pyecharts对收录量数据进行可视化:

  1. import matplotlib.pyplot as plt
  2. from pyecharts import options as opts
  3. from pyecharts.charts import Line
  4. def visualize_index_trend(data_file):
  5. """绘制收录量趋势图"""
  6. # 读取数据
  7. df = pd.read_csv(data_file)
  8. # 按日期分组汇总
  9. df['date'] = pd.to_datetime(df['timestamp']).dt.date
  10. trend_data = df.groupby('date')['index_count'].sum().reset_index()
  11. # 使用matplotlib绘制
  12. plt.figure(figsize=(12, 6))
  13. plt.plot(trend_data['date'], trend_data['index_count'], marker='o')
  14. plt.title('百度收录量趋势图')
  15. plt.xlabel('日期')
  16. plt.ylabel('收录量')
  17. plt.grid(True)
  18. plt.xticks(rotation=45)
  19. plt.tight_layout()
  20. plt.savefig('index_trend.png')
  21. plt.show()
  22. # 使用pyecharts绘制交互式图表
  23. line = (
  24. Line()
  25. .add_xaxis(trend_data['date'].astype(str).tolist())
  26. .add_yaxis("收录量", trend_data['index_count'].tolist(), is_smooth=True)
  27. .set_global_opts(
  28. title_opts=opts.TitleOpts(title="百度收录量趋势图"),
  29. toolbox_opts=opts.ToolboxOpts(is_show=True),
  30. xaxis_opts=opts.AxisOpts(name="日期"),
  31. yaxis_opts=opts.AxisOpts(name="收录量"),
  32. tooltip_opts=opts.TooltipOpts(trigger="axis")
  33. )
  34. )
  35. line.render("index_trend.html")

五、注意事项与合规提醒

5.1 百度规则遵守

  • 请勿频繁请求(建议每次请求间隔不少于 3 秒)

  • 不要使用爬虫抓取非公开数据

  • 尊重百度的 robots.txt 协议

5.2 法律风险提示

  • 本工具仅用于个人或企业对自有网站的收录量监控

  • 禁止用于抓取他人网站数据或进行商业用途

  • 因不当使用导致的任何法律后果由使用者自行承担

5.3 技术局限性

  • 百度收录量显示存在延迟,可能与实际收录情况有差异

  • 部分新发布内容可能未及时显示在收录量中

  • 百度可能随时变更搜索结果页面结构,导致抓取规则失效

通过以上方法,你可以轻松实现百度收录量的自动化监控,为内容创作和 SEO 优化提供数据支持。记得定期检查代码是否正常工作,并根据百度页面结构的变化及时调整解析规则。如果你在使用过程中遇到问题,欢迎在评论区交流讨论!