2022年 11月 9日

Python爬虫之链家二手房数据爬取

Python 依赖模块:

  • requests
  • parsel
  • csv

功能要求:

请求网页

打开开发者工具( F12或者鼠标右键点击检查 )选择 notework 查看数据返回的内容。

 通过开发者工具可以看到,网站是静态网页数据,请求url地址是可以直接获取数据内容的。

  1. url = 'https://cs.lianjia.com/ershoufang/'
  2. headers = {
  3. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
  4. 'Chrome/81.0.4044.138 '
  5. 'Safari/537.36 '
  6. }
  7. response = requests.get(url=url, headers=headers)
  8. print(response.text)

解析数据

网站是静态网页数据,那么就可以直接在开发者工具中 Elements 查看数据在哪

 如上图所示,相关的数据内容都包含在 li 标签里面。通过 parsel 解析库,进行解析提取数据就可以了。

  1. selector = parsel.Selector(response.text)
  2. lis = selector.css('.sellListContent li')
  3. for li in lis:
  4. # 标题
  5. title = li.css('.title a::text').get()
  6. # 地址
  7. positionInfo = li.css('.positionInfo a::text').getall()
  8. community = ''
  9. address = ''
  10. if len(positionInfo):
  11. # 小区
  12. community = positionInfo[0]
  13. # 地名
  14. address = positionInfo[1]
  15. # 房子基本信息
  16. houseInfo = li.css('.houseInfo::text').get()
  17. # 房价
  18. print('数据类型:', type(li.css('.totalPrice span::text').get()))
  19. txt = li.css('.totalPrice span::text').get()
  20. Price = ''
  21. if isinstance(txt, str):
  22. Price = li.css('.totalPrice span::text').get() + '万'
  23. # 单价
  24. print('单价数据类型:', type(li.css('.unitPrice span::text').get()))
  25. txt = li.css('.unitPrice span::text').get()
  26. unitPrice = ''
  27. if isinstance(txt, str):
  28. unitPrice = li.css('.unitPrice span::text').get().replace('单价', '')
  29. # 发布信息
  30. followInfo = li.css('.followInfo::text').get()
  31. dit = {
  32. '标题': title,
  33. '小区': community,
  34. '地名': address,
  35. '房子基本信息': houseInfo,
  36. '房价': Price,
  37. '单价': unitPrice,
  38. '发布信息': followInfo,
  39. }
  40. print(dit)

保存数据(数据持久化)

使用csv模块,把数据保存到Excel里面

  1. # 创建文件
  2. f = open('长沙二手房数据.csv', mode='a', encoding='utf-8', newline='')
  3. csv_writer = csv.DictWriter(f, fieldnames=['标题', '小区', '地名', '房子基本信息',
  4. '房价', '单价', '发布信息'])
  5. # 写入表头
  6. csv_writer.writeheader()
  7. '''
  8. '''
  9. csv_writer.writerow(dit)

多页爬取

  1. for page in range(1, 101):
  2. url = 'https://cs.lianjia.com/ershoufang/'
  3. downloadLianjia(url)
  4. def downloadLianjia(url):
  5. headers = {
  6. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
  7. 'Chrome/81.0.4044.138 '
  8. 'Safari/537.36 '
  9. }
  10. response = requests.get(url=url, headers=headers)
  11. print(response.text)
  12. selector = parsel.Selector(response.text)
  13. lis = selector.css('.sellListContent li')
  14. for li in lis:
  15. # 标题
  16. title = li.css('.title a::text').get()
  17. # 地址
  18. positionInfo = li.css('.positionInfo a::text').getall()
  19. community = ''
  20. address = ''
  21. if len(positionInfo):
  22. # 小区
  23. community = positionInfo[0]
  24. # 地名
  25. address = positionInfo[1]
  26. # 房子基本信息
  27. houseInfo = li.css('.houseInfo::text').get()
  28. # 房价
  29. print('数据类型:', type(li.css('.totalPrice span::text').get()))
  30. txt = li.css('.totalPrice span::text').get()
  31. Price = ''
  32. if isinstance(txt, str):
  33. Price = li.css('.totalPrice span::text').get() + '万'
  34. # 单价
  35. print('单价数据类型:', type(li.css('.unitPrice span::text').get()))
  36. txt = li.css('.unitPrice span::text').get()
  37. unitPrice = ''
  38. if isinstance(txt, str):
  39. unitPrice = li.css('.unitPrice span::text').get().replace('单价', '')
  40. # 发布信息
  41. followInfo = li.css('.followInfo::text').get()
  42. dit = {
  43. '标题': title,
  44. '小区': community,
  45. '地名': address,
  46. '房子基本信息': houseInfo,
  47. '房价': Price,
  48. '单价': unitPrice,
  49. '发布信息': followInfo,
  50. }
  51. print(dit)
  52. # 创建文件
  53. f = open('长沙二手房数据.csv', mode='a', encoding='utf-8', newline='')
  54. csv_writer = csv.DictWriter(f, fieldnames=['标题', '小区', '地名', '房子基本信息',
  55. '房价', '单价', '发布信息'])
  56. # 写入表头
  57. csv_writer.writeheader()
  58. '''
  59. '''
  60. csv_writer.writerow(dit)

效果展示: