html - python xpath爬虫获得新浪微博博主昵称为空
PHPz
PHPz 2017-04-18 09:56:51
0
1
531

第一次用xpath写的爬虫,想获取某人关注列表的每位博主的昵称,但是用下面的代码得到的li永远是空的,为什么捏?

    for i in range(1, pagenum + 1):
        urli = "http://weibo.cn/%d/follow?page=%d"%(uid,i)
        html_sample = requests.get(urli, cookies=cookie).content
        # 使用xpath获取所有昵称
        selector = etree.HTML(html_sample)
        list = selector.xpath('//table/tbody/tr/td[2]/a[1]/text()')
        for li in list:
            print nums,li
            nums += 1
PHPz
PHPz

学习是最好的投资!

reply all(1)
刘奇

If you open the source code of the page and take a look, you will see that the structure is slightly different from the screenshot you took,

    for name in selector.xpath('//table/tr/td/a[1]/text()'):
        print(name)

It’s also very convenient to use BeautifulSoup

    for name in soup.find_all('table'):
        print(name.find_all('a')[1].get_text())
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template