在做老师交代的爬虫小实验时遇到的TypeError: 'str' object is not callable,在网上查询说是有变量与自带的函数冲突了,在改变大部分变量后依然存在这个问题,百思不得其解,想请大家帮忙看一下究竟问题出在哪里
用代码块功能插入代码,请勿粘贴截图
import requests
from bs4 import BeautifulSoup
f_count = 0 #用来记录爬取多少套房子
def get_page_source(url):
global f_count #用于统计爬取房子数目
headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
# 防盗链
#, "referer": "https://sh.esf.fang.com/"
}
resp = requests.get(url, headers=headers)
resp.encoding = "utf-8"
print("页面状态码:{0}".format(resp.status_code))
soup = BeautifulSoup(resp.text, "html.parser") #拿到反爬前页面源代码
tag = soup.find_all("script")[3]
a = str(tag).find("rfss")
var_t4 = url
var_t3 = str(tag)[a:a + 28]
print(type(var_t3))
newUrl = var_t4 + "?" + var_t3
print("当前访问为 {0}:".format(newUrl))
newPage = requests.get(newUrl, headers=headers)
newSoup = BeautifulSoup(newPage.text, "html.parser")
fang_table = newSoup.find("div", attrs={"class": "shop_list_4"}) #拿到页面中房子总框
#print(table)
print(type(fang_table))
fang_list = fang_table.find_all("dl")
print(type(fang_list))
for f in fang_list:
print(f)
print(type(f))
mingcheng = f.find("span", attrs={"class": "tit_shop"})
if not mingcheng:
continue
f_count += 1
# 拿取 几室几厅 面积 朝向
neirong = f.find("p", attrs={"class": "tel_shop"}).text()
print(type(neirong))
tingshi = neirong.split('|')[0].replace(" ", "")
mianji = neirong.split('|')[1].replace(" ", "")
chaoxiang = neirong.split('|')[3].replace(" ", "")
# 拿取房子 所在小区 位置
zong_weizhi = f.find("p", attrs={"class": "add_shop"})
xiaoqu = zong_weizhi.find("a").text()
weizhi = zong_weizhi.find("span").text()
# 拿取 价格 每平方米价格
zong_price = f.find("dd", attrs={"class": "price_right"})
price = zong_price.find("span").text()
avg_price = zong_price.find_all("span").text()
print(mingcheng, tingshi, mianji, chaoxiang, xiaoqu, weizhi, price, avg_price)
def main():
url = "https://sh.esf.fang.com"
n = int(input("请输入要爬取的页数:"))
#https://sh.esf.fang.com/house/i34/ 第几页的网址+i3n
for page in range(1, n+1):
page_url = url + f"/house/i3{page}/"
print(page_url)
# 1, 提取页面源代码
get_page_source(page_url)
print(f"共计爬取{f_count}套房子")
if __name__ == '__main__':
main()
运行结果及报错内容 这里是报错内容
Traceback (most recent call last):
File "E:/pythonproject/fangpachong2.py", line 84, in <module>
main()
File "E:/pythonproject/fangpachong2.py", line 78, in main
get_page_source(page_url)
File "E:/pythonproject/fangpachong2.py", line 43, in get_page_source
neirong = f.find("p", attrs={"class": "tel_shop"}).text()
TypeError: 'str' object is not callable