Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
我想爬取https://sh.ke.com/ershoufang/上面的二手房交易信息,请帮我用python写出具体的代码
- User
- Assistant
由于网站反爬虫机制的存在,爬取网站数据可能会受到限制或封禁 IP,因此在爬取数据时请注意网站的 robots.txt 文件,并尊重网站的反爬虫策略。
以下是一个简单的 Python 爬虫示例,使用 requests 和 BeautifulSoup 库来爬取链家网上的二手房信息:
pythonimport requests from bs4 import BeautifulSoup # 目标网页 URL url = "https://sh.ke.com/ershoufang/" # 请求头部信息,包括 User-Agent,模拟浏览器访问 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) # 使用 BeautifulSoup 解析网页内容 soup = BeautifulSoup(response.text, 'html.parser') # 查找二手房信息的 HTML 元素 house_list = soup.find_all('div', class_='info') # 遍历房屋信息,并打印出来 for house in house_list: title = house.find('div', class_='title').a.text.strip() price = house.find('div', class_='price').span.text.strip() address = house.find('div', class_='address').text.strip() print(title, price, address)