V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
funboy
V2EX  ›  Python

昨晚心血来潮写了一段代码抓取房天下的广州楼盘信息,但一直抓不到数据,请高手们帮忙看看

  •  1
     
  •   funboy · 2018-09-23 23:26:19 +08:00 · 1705 次点击
    这是一个创建于 2035 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近学习数据挖掘,用 python 写了一个程序,使用 lxml 和 requests 爬 http://gz.newhouse.fang.com/house/s/b91/这个网页,网页爬下来后,用 xpath 挑选数据集。看网页源码可以看到每个楼盘信息的数据分段都是在标签"<div class=clearfix>"里,但是 python 中用 xpath('//div[@class="clearfix"]')语句,却只返回空表。请各位帮忙看看,是哪里错了?



    for index in range(28):
    url = 'http://gz.newhouse.fang.com/house/s/b9{}'.format(index)
    con=con+requests.get(url).content
    sel = html.fromstring(con)
    print("Read!")

    print("Writting",end="")



    #获得楼盘信息的数据集合
    for i in sel.xpath('//div[@class="clearfix"]'):
    # 楼盘名称
    name = "".join(i.xpath('div[@class="div class="house_value clearfix""]/div/a/text()')).split()
    print(name)

    print(".",end="")

    print("Done!")
    3 条回复    2018-09-25 17:48:02 +08:00
    ClutchBear
        1
    ClutchBear  
       2018-09-24 00:34:49 +08:00
    问题是你这个代码根本没法运行啊.
    con 是啥
    html 是啥.
    根本没说.
    在知乎看到过了, 我专门拷贝下来想运行一下的.
    funboy
        2
    funboy  
    OP
       2018-09-24 14:12:37 +08:00
    @ClutchBear 对不起,我没留意到发帖时候抓屏不全

    import requests
    import pymysql
    from lxml import html

    db=pymysql.connect(host='localhost',port=3306,user='dbuser',passwd='wilson',db='mydb',charset='utf8')
    cursor = db.cursor()
    #ursor.execute("TRUNCATE TABLE `gz_newhouse`")
    #b.commit()
    con=b''
    wersonliu9527
        3
    wersonliu9527  
       2018-09-25 17:48:02 +08:00
    先取 这个 //*[@id="newhouse_loupai_list"]/ul/li

    再循环
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1462 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:18 · PVG 01:18 · LAX 10:18 · JFK 13:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.