python爬虫爬取58同城信息(使用动态IP)

新手,为了做一个数据分析,搞了几天,终于搞出来了,大家可以给点意见啊。

# coding=utf-8
import sys
import csv
import time
from importlib import reload
import xlwt
import requests
from bs4 import BeautifulSoup
from goto import with_goto
reload(sys)
# 请求头设置
#使用win自带的浏览器的agent
User_Agent = 'your-agent'
headers = {'User-Agent': User_Agent,
}
def download(url):File = open('filename', 'w')excel = xlwt.Workbook(encoding='utf-8')sheet = excel.add_sheet('shee1')sheet.write(0, 0, '标题')sheet.write(0, 1, '房子')sheet.write(0, 2, '大小')sheet.write(0, 3, '地址1')sheet.write(0, 4, '地址2')sheet.write(0, 5, '其他')sheet.write(0, 6, '价格')sheet.write(0, 7, '地区')sheet.write(0, 8, '其他2')db_data = requests.get(url)soup = BeautifulSoup(db_data.text, 'lxml')#url1 = soup.select()#print(soup)titles = soup.select('body > div.list-wrap > div.list-box > ul > li > div.des > h2 > a:nth-of-type(1)')#print(type(titles[0].spilt('\"')))#print(titles)houses = soup.select('body > div.list-wrap > div.list-box > ul > li > div.des > p.room')oneaddresss = soup.select('body > div.list-wrap > div.list-box > ul > li > div.des > p.infor > a:nth-of-type(1)')twoaddresss = soup.select('body > div.list-wrap > div.list-box > ul > li > div.des > p.infor > a:nth-of-type(2)')additions = soup.select('body > div.list-wrap > div.list-box > ul > li > div.des > p.infor')#print(additions[0])prices = soup.select('body > div.list-wrap > div.list-box > ul > li > div.list-li-right > div.money > b')#print(houses[0])#print(type(str(titles[0].string).replace(' ', '').replace('\n', '')))'''for i in range(100):print(i)print(addition[0].get_text().split(' ')[i].replace(' ', '').replace("\n", ""))'''count = 1ssss = 5for title, house, oneaddress, twoaddress, addition, price in zip(titles, houses, oneaddresss, twoaddresss, additions, prices):data = [(str(title.string).replace(' ', '').replace('\n', ''),house.get_text().split(' ')[0].replace(' ', '').replace("\n", ""),house.get_text().split(' ')[20].replace(' ', '').replace("\n", "").replace("\xa0", ""),oneaddress.get_text().replace(' ', '').replace("\n", ""),twoaddress.get_text().replace(' ', '').replace("\n", ""),addition.get_text().split(' ')[64].replace(' ', '').replace("\n", ""),price.get_text().replace(' ', '').replace("\n", ""))]sheet.write(count, 0, str(title.string).replace(' ', '').replace('\n', ''))sheet.write(count, 1, house.get_text().split(' ')[0].replace(' ', '').replace("\n", ""))sheet.write(count, 2, house.get_text().split(' ')[20].replace(' ', '').replace("\n", "").replace("\xa0", ""))sheet.write(count, 3, oneaddress.get_text().replace(' ', '').replace("\n", ""))sheet.write(count, 4, twoaddress.get_text().replace(' ', '').replace("\n", ""))sheet.write(count, 5, addition.get_text().split(' ')[64].replace(' ', '').replace("\n", ""))sheet.write(count, 6, price.get_text().replace(' ', '').replace("\n", ""))url1 = str(title).split('\"')[3]time.sleep(5)try:db_data1 = requests.get(url1, headers=headers)except:time.sleep(10)try:db_data1 = requests.get(url1, headers=headers)except:time.sleep(35)try:db_data1 = requests.get(url1, headers=headers)except:excel.save('filename.xls')soup1 = BeautifulSoup(db_data1.text, 'lxml')#print(soup1)#print(soup1)pos = soup1.select('body > div.houseInfo > ul >li')try:print(pos[2].get_text().split(' ')[28].replace(' ', '').replace("\n", ""))except:excel.save('filename.xls')sheet.write(count, 7, pos[2].get_text().split(' ')[28].replace(' ', '').replace("\n", ""))pos2 = soup1.select('body > div.configure > div.fang-detail > dl> dd> ul >li')print(pos2)stra = ''if len(pos2):for p in pos2:if (len(str(p)) < 25):print(p.get_text().replace(' ', '').replace("\n", ""))stra = stra + ',' + p.get_text().replace(' ', '').replace("\n", "")# print((str(p)))# print(len(str(p)))pos3 = soup1.select('body > div.configure > ul >li')for q in pos3:if (len(str(q)) < 25):print(q.get_text().replace(' ', '').replace("\n", ""))stra = stra + ',' + q.get_text().replace(' ', '').replace("\n", "")print(stra)sheet.write(count, 8, stra)else:pos2 = soup1.select('body > div.configure > ul >li')print(pos2)for p in pos2:if (len(str(p)) < 25):print(p.get_text().replace(' ', '').replace("\n", ""))print(len(str(p)))stra = stra + ',' + p.get_text().replace(' ', '').replace("\n", "")print(stra)sheet.write(count, 8, stra)#print(type(house.get_text().split(' ')[0].replace(' ', '').replace("\n", "")))#print(data)#print("已经抓取" + str(count) + "条数据")count += 1print(count)time.sleep(ssss)excel.save('filename.xls')if __name__ == '__main__':download("https://cd.58.com/chuzu/pn6/?PGTID=0d3090a7-0006-6d54-6db2-a757b630de63&ClickID=2")

由于58同城的反扒机制,使用自己的IP只能爬取几个数据就被封了,建议使用动态IP。
话不多说,直接上图。

python爬虫爬取58同城租房信息(使用动态IP)输出Excel文件相关推荐

  1. python爬取58同城租房信息_分页爬取58同城租房信息.py

    import requests,re,openpyxl,os headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleW ...

  2. python爬虫爬取豆瓣电影排行榜并通过pandas保存到Excel文件当中

    我们的需求是利用python爬虫爬取豆瓣电影排行榜数据,并将数据通过pandas保存到Excel文件当中(步骤详细) 我们用到的第三方库如下所示: import requests import pan ...

  3. python爬取58同城租房信息_python爬虫:找房助手V1.0-爬取58同城租房信息(示例代码)...

    #!/usr/bin/python # -*- encoding:utf-8 -*-importrequests frombs4 importBeautifulSoup frommultiproces ...

  4. python爬取58同城租房信息,用selenium爬取58同城租房信息(万级数据)

    今天想做一个58同城的爬虫,然后到页面分析一下链接,发现58同城的链接的有些参数是由js动态生成的,然后我就想偷懒了.(当然其实去js文件中找到生成式并不难),但我就是不想去找.然后就想到了selen ...

  5. python爬虫--爬取链家租房信息

    python 爬虫-链家租房信息 爬虫,其实就是爬取web页面上的信息. 链家租房信息页面如下: https://gz.lianjia.com/zufang/ ## python库 Python库 1 ...

  6. python爬虫爬取58同城北京品牌公寓租房信息

    from bs4 import BeautifulSoup from urllib.parse import urljoin import requests import csvurl = " ...

  7. python爬取58同城租房信息

    代码: # coding=utf-8 import sys import csv import requests from bs4 import BeautifulSoup ''' 遇到不懂的问题?P ...

  8. 利用python爬取租房信息_python爬取58同城租房信息

    代码: # coding=utf-8 import sys import csv import requests from bs4 import BeautifulSoup ''' 遇到不懂的问题?P ...

  9. 利用scrapy爬取58同城租房信息

    tc.py 代码 # -*- coding: utf-8 -*- import scrapy from ..items import TcItemclass Tc58Spider(scrapy.Spi ...

最新文章

  1. oracle plsql开启并行,Oracle开启并行的几种方法
  2. 10自带sftp服务器_一文讲透FTP和SFTP的区别
  3. python爬虫入门代码-Python爬虫入门(一) 网络爬虫之规则
  4. 特斯拉与Mobileye口水战的背后,是自动驾驶技术话语权之争
  5. Git Gitlab 使用指南之TortoiseGit
  6. 存储型xss漏洞怎么解决_FinDOMXSS:一款针对DOM型XSS漏洞的快速扫描工具
  7. Asp.Net微信发布菜单,出现“invalid sub button url domain hint”错误
  8. 数学 —— 计算几何 —— 平面分割问题
  9. CXF整合Spring开发WebService
  10. 怎么计算z=x+y的概率密度_上大《Scripta Mater》基于第一性原理计算,研究镁合金强化相!...
  11. leetcode题库572 -- 另一个树的子树
  12. word转化为pdf android,如何将Word转化为PDF?
  13. 自己动手,丰衣足食。普通键盘实现键盘宏(Windows和Mac版)
  14. 关于程序员的冷笑话 (转载www.lenxiaohuadaquan.net )
  15. 北理校园网省流攻略之下载篇
  16. AJP:斯坦福加速智能神经调控疗法治疗难治性抑郁症
  17. 《算法竞赛》被评为清华大学出版社2022年度“十佳图书”
  18. sftp stp差异
  19. python pandas 条件选择
  20. 403 Forbidden vs 401 Unauthorized HTTP 响应

热门文章

  1. python二手交易平台代码_PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)...
  2. 华为终端中国区手机销量已达千万,80%为智能机
  3. oracle SQL性能分析之10053事件
  4. 微信上谁偷偷把你删了?用Python教你一分钟检验出来!不用群发
  5. Dm9000aep在u-boot下网卡驱动调试笔记
  6. 【LeetCode】最近公共祖先问题
  7. 智能市场红利当头,科技应以人为本
  8. 我被google面试后——回答“谷歌如何打败百度?”
  9. Windows记事本UTF-8编码异常
  10. 我是如何建立一个Google代理搜索服务(GuSou)的