由于出现以下错误,我无法使用bs4从此网页提取数据
"AttributeError: 'NoneType' object has no attribute 'text'"有人可以修改我的代码吗?
以下是我的代码
from bs4 import BeautifulSoup
import requests
url = 'https://e-masjid.jais.gov.my/index.php/profail?page=1'
html_content = requests.get(url).text
soup = BeautifulSoup(html_content, 'lxml')
masjid_table = soup.find("table", attrs={"class": "Masjid"})
masjid_table_data = masjid_table.tbody.find_all("tr")
headings = []
for td in masjid_table_data[0].find_all("td"):
headings.append(td.b.text.replace('\n', ' ').strip())
print(headings)masjid_table = soup.find("table", attrs={"class": "Masjid"})
masjid_table_data = masjid_table.tbody.find_all("tr")
headings = []
for td in masjid_table_data[0].find_all("td"):
headings.append(td.b.text.replace('\n', ' ').strip())
print(headings)发布于 2020-06-19 12:22:23
虽然我不知道你想要的输出是多少..这将为您提供所有的masjid名称..
soup = BeautifulSoup(html_content.text, 'lxml')
masjid_table = soup.find("table", attrs={"id": "pemohon"})
masjid_table_data = masjid_table.tbody.find_all("tr")
headings = []
for row in masjid_table_data:
headings.append(row.find_all('td')[1].text.replace('\nMASJID', ' ').strip())您可以尝试在row.find_all('td')[1]中更改索引,以获得相应的列。
发布于 2020-06-19 12:22:49
属性错误意味着您正在调用给定对象不存在的属性。在您的示例中,requests.get(url)返回None,因此requests.get(url).text与None.text相同,这是无效的。如果这是预期的,请检查请求是否返回了某些内容:
result = requests.get(url)
if result:
string = requests.get(url).texthttps://stackoverflow.com/questions/62462860
复制相似问题