使用 beautifulsoup 从带有 ID 的网站进行表抓取

Ach*_*les 5 python beautifulsoup web-scraping python-3.x python-requests

我在抓取该网站的表格时遇到问题,我应该得到标题,但我却得到了

AttributeError: 'NoneType' object has no attribute 'tbody'
Run Code Online (Sandbox Code Playgroud)

我对网络抓取有点陌生,所以如果你能帮助我那就太好了

import requests
from bs4 import BeautifulSoup

URL = "https://www.collincad.org/propertysearch?situs_street=Willowgate&situs_street_suffix" \
      "=&isd%5B%5D=any&city%5B%5D=any&prop_type%5B%5D=R&prop_type%5B%5D=P&prop_type%5B%5D=MH&active%5B%5D=1&year=2021&sort=G&page_number=1"

s = requests.Session()

page = s.get(URL)
soup = BeautifulSoup(page.content, "lxml")

table = soup.find("table", id="propertysearchresults")
table_data = table.tbody.find_all("tr")

headings = []
for td in table_data[0].find_all("td"):
    headings.append(td.b.text.replace('\n', ' ').strip())

print(headings)
Run Code Online (Sandbox Code Playgroud)

Hed*_*Hog 2

会发生什么?

\n

注意: 一定要先看你的汤——这就是真相。内容始终可能与开发工具中的视图略有不同或极其不同。

\n
\n

访问权限被撤销

\n

您的 IP 地址已被阻止。

我们\n检测到来自您的 IP 地址\n对我们的财产搜索的不正常、类似机器人的使用。设立此块是为了减轻我们网络服务器的压力,以确保我们为科林县的纳税人提供最佳的网站性能。

我们没有阻止\n您下载的能力我们的\n数据导出,您仍然可以使用它来获取批量属性\n数据。

\n
\n

怎么修?

\n

在您的请求中添加一个user-agent,使其看起来像您使用“浏览器”进行的请求。

\n
headers = {\'user-agent\': \'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36\'}\npage = s.get(URL,headers=headers)\n
Run Code Online (Sandbox Code Playgroud)\n

或者作为替代方案,只需下载结果。

\n

示例(抓取表)

\n
import requests\nfrom bs4 import BeautifulSoup\nimport pandas as pd\n\nheaders = {\'user-agent\': \'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36\'}\n\nURL = "https://www.collincad.org/propertysearch?situs_street=Willowgate&situs_street_suffix" \\\n      "=&isd%5B%5D=any&city%5B%5D=any&prop_type%5B%5D=R&prop_type%5B%5D=P&prop_type%5B%5D=MH&active%5B%5D=1&year=2021&sort=G&page_number=1"\n\ns = requests.Session()\n\npage = s.get(URL,headers=headers)\nsoup = BeautifulSoup(page.content, "lxml")\n\ndata = []\nfor row in soup.select(\'#propertysearchresults tr\'):\n    data.append([c.get_text(\' \',strip=True) for c in row.select(\'td\')])\n\npd.DataFrame(data[1:], columns=data[0])\n
Run Code Online (Sandbox Code Playgroud)\n

输出

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n
属性 ID \xe2\x86\x93 地理 ID \xe2\x86\x93业主姓名物业地址法律说明2021年市场价值
12709013 R-10644-00H-0010-1帕塔萨拉西·苏雷什和安妮莎·哈里克里希南12209 Willowgate 弗里斯科博士,德克萨斯州\\xa0 75035Ridgeview At Panther Creek 2 期,H 座,地块 1513,019 美元
22709018 R-10644-00H-0020-1乔希·普拉尚特 (Joshi Prashant) & 施韦塔 (Shweta) 裤子12235 Willowgate 弗里斯科博士,德克萨斯州\\xa0 75035Ridgeview At Panther Creek 2 期,H 座,2 号地块$546,254
32709019 R-10644-00H-0030-1塔拉普雷迪·拉文德拉 & 乌玛·马赫斯瓦里·韦穆拉12261 Willowgate 弗里斯科博士,德克萨斯州\\xa0 75035Ridgeview At Panther Creek 2 期,H 座,地块 3$550,768
42709020 R-10644-00H-0040-1KULKARNI BHEEMSEN T 和 GURI R12287 Willowgate 弗里斯科博士,德克萨斯州\\xa0 75035Ridgeview At Panther Creek 2 期,H 座,4 号地块509,593 美元
52709021 R-10644-00H-0050-1巴拉姆·加内什和桑蒂雷卡·洛库拉12313 Willowgate 弗里斯科博士,德克萨斯州\\xa0 75035Ridgeview At Panther Creek 2 期,H 座,地块 5$553,949
\n
\n

...

\n