【问题标题】:BeautifulSoup not fetching the DataBeautifulSoup 没有获取数据
【发布时间】:2019-12-19 06:59:56
【问题描述】:

我正在尝试从website 获取数据。但在变量汤中没有获得任何字段的信息,如姓名、业务性质、电话、电子邮件等。我应该在下面的代码中添加什么来获得这些数据?

import requests 
import pandas as pd
from bs4 import BeautifulSoup
page = "http://www.pmas.sg/page/members-directory"
pages = requests.get(page)
soup = BeautifulSoup(pages.content, 'html.parser')
print(soup)

我使用上面的代码得到的输出是:-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<html>
<head>
<title>WebKnight Application Firewall Alert</title>
<meta content="NOINDEX" name="ROBOTS"/>
</head>
<body bgcolor="#ffffff" link="#FF3300" text="#000000" vlink="#FF3300">
<table cellpadding="3" cellspacing="5" width="410">
<tr>
<td align="left">
<font face="Verdana,Arial,Helvetica" size="2">
<font size="3"><b>WebKnight Application Firewall Alert</b></font><br/><br/><br/>
Your request triggered an alert! If you feel that you have received this page in error, please contact the administrator of this web site.
<br/>
<hr/>
<br/><b>What is WebKnight?</b><br/>
AQTRONIX WebKnight is an application firewall for web servers and is released under the GNU General Public License. It is an ISAPI filter for securing web servers by blocking certain requests. If an alert is triggered WebKnight will take over and protect the web server.<br/><br/>
<hr/>
<br/>For more information on WebKnight: <a href="http://www.aqtronix.com/webknight/">http://www.aqtronix.com/WebKnight/</a><br/><br/>
<b><font color="#FF3300">AQTRONIX</font> WebKnight</b></font>
</td>
</tr>
</table>
</body>
</html>

【问题讨论】:

  • 这是关于标题的。因为实际上你得到了999的响应代码,所以你只需要user-agent。在下面查看我的答案

标签: python-3.x web-scraping beautifulsoup


【解决方案1】:
import requests
from bs4 import BeautifulSoup
import csv
import regex

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0"
}
r = requests.get('http://www.pmas.sg/page/members-directory', headers=headers)

soup = BeautifulSoup(r.text, 'html.parser')

data = []
for item in soup.findAll('div', {'class': 'col-md-4'}):
    l = []
    for p in item.findAll('p'):
        matches = regex.findall(
            r"^(?:.*?:[[:blank:]]+\K)?.*", p.text, regex.MULTILINE)
        b = next(iter(matches))
        l.append(b)
    if l:
        print(l)
        data.append(l)


with open('data.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['Name', 'Nature of Business',
                     'Address', 'Contact', 'Phone#', 'Fax', 'Website', 'Email'])
    writer.writerows(data)
    print("Done")

【讨论】:

  • 感谢您的回复,但脚本现在以文本形式出现,我如何从该文本中提取姓名、电子邮件和电话等,对不起,我对 python 了解甚少
  • @renu 我相信您希望拥有csv 格式的它。所以你现在有了它。
  • 非常感谢!!这对我帮助很大
  • 很高兴帮助@renu
【解决方案2】:

WebKnight 是 an ISAPI filter that secures your web server by blocking certain requests。服务器管理员设置了适用于传入请求并确定是否阻止的规则。在这种情况下,规则包括对允许(和必需)User-Agent 标头的期望。我注意到在玩耍:

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)' 或 5.0 变体触发警报

'Mozilla/4.0 (Windows NT 10.0; WOW64)', 'AppleWebKit/537.36 (KHTML, like Gecko)' , 'Chrome/79.0.3945.79' , 'Safari/537.36' 都很好,所以看起来列表可能需要在服务器上更新。

请注意,&lt;META NAME="ROBOTS" CONTENT="NOINDEX"&gt; 将索引指示为不需要,但我找不到任何 T&C,并且没有用于管理抓取的 robots.txt 文件。

例如

import requests

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

r = requests.get('http://www.pmas.sg/page/members-directory', headers=headers)
print(r.text)

【讨论】:

  • 感谢您的回复,但脚本现在以文本形式出现,我如何从该文本中提取姓名、电子邮件和电话等,对不起,我对 python 了解甚少
  • 我会让艾哈迈德处理这个问题,如果我看到另一种方式向您展示用于抓取的笔记,我将编辑答案。
猜你喜欢
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
相关资源
最近更新 更多