info = house.find('div', class_='houseInfo').text.strip().split(' ', '|')有什么错误
时间: 2024-01-27 20:05:48 浏览: 17
这段代码没有语法错误,但是可能会出现以下问题:
1. 如果在代码中使用了`house`变量,但是没有给出其定义,那么这段代码就会报错。
2. 如果`house`变量所指向的HTML页面中没有`div`标签且class属性值为`houseInfo`,那么调用`find`方法将会返回`None`,接着调用`text`、`strip`和`split`方法就会出错。
相关问题
python爬取链家二手房信息400条,并存入house.xlsx表格中
好的,我可以为您提供以下代码来实现这个爬取任务。不过需要注意的是,网站的反爬虫机制比较严格,如果爬取过于频繁会被封IP,建议加上一些延时等措施。
```
import requests
import openpyxl
from bs4 import BeautifulSoup
import time
url = "https://bj.lianjia.com/ershoufang/pg"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = "house"
sheet['A1'] = '小区名称'
sheet['B1'] = '户型'
sheet['C1'] = '面积'
sheet['D1'] = '朝向'
sheet['E1'] = '楼层'
sheet['F1'] = '年代'
sheet['G1'] = '总价(万)'
sheet['H1'] = '单价(元/平米)'
row = 2
for i in range(1, 21):
print("正在爬取第{}页数据".format(i))
res = requests.get(url + str(i), headers=headers)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
house_list = soup.find_all('div', {'class': 'info clear'})
for house in house_list:
name = house.find('div', {'class': 'title'}).a.get_text()
room = house.find('div', {'class': 'houseInfo'}).get_text().split('|')[1].strip()
square = house.find('div', {'class': 'houseInfo'}).get_text().split('|')[2].strip()
direction = house.find('div', {'class': 'houseInfo'}).get_text().split('|')[3].strip()
floor = house.find('div', {'class': 'positionInfo'}).get_text().split('-')[1].strip()
year = house.find('div', {'class': 'positionInfo'}).get_text().split('-')[0].strip()
total_price = house.find('div', {'class': 'totalPrice'}).span.get_text()
unit_price = house.find('div', {'class': 'unitPrice'}).get_text().strip()[2:-4]
sheet.cell(row=row, column=1, value=name)
sheet.cell(row=row, column=2, value=room)
sheet.cell(row=row, column=3, value=square)
sheet.cell(row=row, column=4, value=direction)
sheet.cell(row=row, column=5, value=floor)
sheet.cell(row=row, column=6, value=year)
sheet.cell(row=row, column=7, value=total_price)
sheet.cell(row=row, column=8, value=unit_price)
row += 1
time.sleep(1)
wb.save('house.xlsx')
print("数据爬取完成!")
```
python爬取济南链家网户型信息代码
以下是使用 Python 爬取济南链家网户型信息的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://jn.lianjia.com/ershoufang/pg1/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
house_items = soup.find_all('div', class_='info clear')
for item in house_items:
title = item.find('div', class_='title').a.text.strip()
price = item.find('div', class_='totalPrice').span.text.strip() + '万'
area = item.find('div', class_='houseInfo').text.strip().split('|')[1].replace('平米', '').strip()
room = item.find('div', class_='houseInfo').text.strip().split('|')[0]
floor = item.find('div', class_='positionInfo').text.strip().split(' ')[0]
year = item.find('div', class_='positionInfo').text.strip().split(' ')[-1].replace('年建', '').strip()
print(title, price, area, room, floor, year)
```
使用 `requests` 库和 `BeautifulSoup` 库分别发送请求和解析 HTML 页面。代码中的 `url` 是济南链家网二手房列表的第一页,可以根据需要修改为其他页面的链接。`headers` 是模拟浏览器发送请求的请求头,需要设置 User-Agent,否则链家网会返回 403 状态码。
解析 HTML 页面后,使用 `find_all` 方法找到所有的房源信息,包括标题、总价、面积、房型、楼层和建造年份。最后将每个房源的信息打印出来。