【问题标题】:BeautifulSoup returns NoneBeautifulSoup 返回无
【发布时间】:2018-10-14 15:19:06
【问题描述】:

我试图在this URL 中获取列表的标题,但此代码返回无。

import requests 
from bs4 import BeautifulSoup  

# get the data 
data = requests.get('https://www.lamudi.com.ph/metro-manila/makati/condominium/buy/')

# Update Header
headers = requests.utils.default_headers()
headers.update({
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) 
Gecko/20100101 Firefox/31.0',
})
# load data into bs4
soup = BeautifulSoup(data.text, 'html.parser')

# We need to extract all the data in this div: <div 
class="ListingCell-KeyInfo-title" ..>

listingsTitle = soup.find('div', { 'class': 'ListingCell-KeyInfo-title'})
print(listingsTitle)

知道为什么吗?

谢谢

【问题讨论】:

  • 在创建的汤中,没有类“ListingCell-KeyInfo-title”的 div,正如@Bob 在下面的回答中所说,因为该网站将您视为机器人,它可能已经删除了一些信息
  • 爬取这个网站比你想象的要难
  • 看起来是这样 :(

标签: python selenium beautifulsoup


【解决方案1】:

您请求的网址将您视为机器人。

请求响应:

h1>Pardon Our Interruption...</h1>
<p>
      As you were browsing <strong>www.lamudi.com.ph</strong> something about your 
browser made us think you were a bot. There are a few reasons this might happen:
        </p>
<ul>

在您从响应中解析任何内容之前。

首先打印内容以确保您可以正确访问网址。

你必须添加 User-Agent 或其他东西才能让你像一个真正的用户

尝试将此添加到您的请求标头中:

USER_AGENT_FIREFOX= 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0'

【讨论】:

  • 我添加了这个但同样的东西。 # Update Header headers = requests.utils.default_headers() headers.update({ 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0', })
【解决方案2】:

我尝试使用 selenium 并进行特定等待,但不起作用。 如果你打印汤,你会得到错误。事实上,该页面返回以下内容:“当您浏览 www.lamudi.com.ph 时,您的浏览器中的某些内容让我们认为您是机器人。这可能有几个原因: ... "

网站承认你不是人类。

import requests 
from bs4 import BeautifulSoup  

# get the data 
data = requests.get('https://www.lamudi.com.ph/metro-manila/makati/condominium/buy/')

# load data into bs4
soup = BeautifulSoup(data.text, 'html.parser')

# We need to extract all the data in this div: <div class="ListingCell-KeyInfo-title" ..>
print(soup)    #--> this print get the error

listingsTitle = soup.find('div', class_='ListingCell-KeyInfo-title')
print(listingsTitle)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2020-11-03
    • 2020-08-02
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多