【问题标题】:BeautifulSoup webpage have protection and prettify() return no dataBeautifulSoup 网页有保护和 prettify() 不返回数据
【发布时间】:2026-02-06 22:00:02
【问题描述】:

我正在尝试从葡萄酒网站获取一些数据。

但我无法评估数据并且有使用违规消息。

网址:https://www.wine-searcher.com/find/drc/2013

prettify() 结果是这样的:

<div id="bodycontainer">
<div class="colmask contentparent">
 <div id="colheader">
  <div class="colmask articlecontainer">
   <div class="colmidtemp3">
    <div class="collefttemp3">
     <div class="col1wraptemp3">
      <div class="col1temp3">
       <div>
        <h1 style="margin:50px 0 0">
         Usage Violation
        </h1>
        <div style="margin-bottom:50px;padding:50px 10px;background-color:#FFFACD">
         <h2 style="font-size:1.4em">
          Blocked
         </h2>
         <p style="font-size:1.2em">
          The IP Address [xx.xxx.xxx.xx] you are using has been used in violation of Wine-Searcher's usage guidelines.
          <b>
           If you think you have received this message in error restart your web browser and retry accessing wine-searcher.com.
          </b>
         </p>
         <p style="font-size:1.2em">
          To re-gain access to Wine-Searcher please
          <a href="mailto:wsexcessiveuse@wine-searcher.com?subject=Blocked IP=1 ID=PVBXC7PJCM80025">
           Contact Us
          </a>
          .
         </p>
        </div>
       </div>
      </div>
     </div>

是否有任何可能的方法从 url 获取数据?非常感谢。

我的编码在这里:

# -*- coding: utf-8 -*-
import bs4
import re
import requests
import sys
from bs4 import BeautifulSoup

name = "Wine.txt"
k = open(name, "w", encoding='utf-8')

Stat_url = "https://www.wine-searcher.com/find/drc/2012"
page = requests.get(Stat_url)
soup = bs4.BeautifulSoup(page.text,'lxml')

k.write(soup.prettify())

【问题讨论】:

  • 您可以发布您发送的请求吗?

标签: python beautifulsoup


【解决方案1】:

看起来他们在他们的页面上添加了一些保护来防止这种情况,你试图做的事情;) 他们通过 API https://www.wine-searcher.com/api.lml 出售访问权限,并允许在 5 天内试用 100 次调用。也许这对你来说已经足够了?

我猜 BeautifulSoup 会在短时间内尝试处理许多请求? (也许将其限制为每 10 秒一次并让它运行一夜?) 可以将 BS 中的 agent-id 更改为更常见的东西,例如常规浏览器 agent-id?

【讨论】:

  • 尝试基本的东西,比如带有适当用户代理的漂亮标题。像用户代理和请求屏幕这样的页面会自行发送“python 请求....”-ish。看看:scrapehero.com/… 如果您需要代理,请从 gimmeproxy 获取。有一个易于使用的免费 api
  • 感谢marcel,当涉及到请求时,无论我提交多少请求,它总是显示相同的阻止结果。你已经说过agent-id可以类似于普通的id。谢谢你的想法,我会试试的。
  • 谢谢jlaur,根据您的评论,如果我想绕过像葡萄酒页面这样的网站,还有其他因素会影响这个问题吗?
  • @Kenny 我评论中的链接正好包含了相关的输入。尝试谷歌搜索(例如google.com/search?q=avoid+ip+blocking+with+web+scraping
最近更新 更多