‘’’
    网站:https://cs.lianjia.com/ershoufang/
    需求:前5页数据 __
    名称 位置 价格 面积 保存到csv

    分析网站:
    二手房:
    第一页:https://cs.lianjia.com/ershoufang/rs/
    也可以改成:https://cs.lianjia.com/ershoufang/pg1/
    第二页:https://cs.lianjia.com/ershoufang/pg2/
    第三页:https://cs.lianjia.com/ershoufang/pg3/
    第四页:https://cs.lianjia.com/ershoufang/pg4/
    第五页:https://cs.lianjia.com/ershoufang/pg5/
    所以可以写成 url = f’https://cs.lianjia.com/ershoufang/pg{page}/‘ 变量page为1到5遍历
    ‘’’_

    1. import csv
    2. import requests
    3. from bs4 import BeautifulSoup
    4. data_list = []
    5. for page in range(1,6):
    6. url = f'https://cs.lianjia.com/ershoufang/pg{page}'
    7. req = requests.get(url)
    8. soup = BeautifulSoup(req.text, 'lxml')
    9. ul = soup.find(class_="sellListContent")
    10. divs = ul.find_all(class_="info clear")
    11. for div in divs:
    12. list1 = {}
    13. title = div.find('div', class_='title').text.replace("必看好房", "")
    14. flood = div.find('div', class_='flood').text
    15. address = div.find('div', class_='address').text.split('|')[:2]
    16. address1 = "|".join(address)
    17. price1 = div.find('div', class_='totalPrice totalPrice2').text
    18. price2 = div.find('div', class_='unitPrice').text
    19. price = price1+"|"+price2
    20. list1['title'] = title
    21. list1['flood'] = flood
    22. list1['address'] = address1
    23. list1['price'] = price
    24. data_list.append(list1)
    25. with open('bs4_20220410_长沙二手房房源.csv', 'w', encoding='utf-8', newline='') as f:
    26. wt = csv.DictWriter(f,fieldnames=['title', 'flood', 'address', 'price'])
    27. wt.writeheader()
    28. wt.writerows(data_list)
    29. print("文件保存成功,任务完成!")

    总结一下:其实用bs4来做真的很爽!看看代码,很清晰的感觉!
    附(bs420220410长沙二手房房源.csv)
    20220411.png