第一次用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
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,
It’s also very convenient to use BeautifulSoup