` 提示:这里我们采用PyCharm 2023.2.1
分析网站
获取网站数据
提取我们想要的数据
保存想要的数据
我们打开二手房价网站,例如(58同城:https://quanguo.58.com/ershoufang/?PGTID=0d100000-0221-8af0-e247-a41467723324&ClickID=4)
创建项目编写程序
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://www.58.com/ershoufang/'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
tbimg = soup.find_all('table', class_='tbimg')
data = []
for table in tbimg:
cleft = table.find_all('tr')
for row in cleft:
name = row.find('a', class_='t').string
price = row.find('b').string
data.append([ name, price])
df = pd.DataFrame(data, columns=['name','price'])
df.to_csv('全国房价信息.csv', index=False, encoding='utf-8')