爬取百度top10热搜

学爬虫的时候无聊练练手
主要用到xpath作为解析网页标签的工具
接下来没啥好说的了
就是简单的爬虫

import requests
from lxml import etree

head = {}
url = "http://top.baidu.com/buzz?b=341&fr=topindex"
head["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:63.0) Gecko/20100101 Firefox/63.0"
head["Accept"]= "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
head["Accept-Language"]= "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2"
head["Connection"] = "keep-alive"
def main(): 
    print("百度热搜top10: ")
    res = requests.get(url , headers = head)
    with open("html.txt", "wb") as f:
        f.write(res.content)
    html = etree.parse('html.txt' , etree.HTMLParser(encoding='gbk'))
    top_list = html.xpath('//a[@class="list-title"]/text()')
    num_search = html.xpath('//span[@class="icon-rise"]/text()')


    for i  , j in zip(top_list[:10] , num_search[:10]):
        print(i ,"搜索指数为:" ,  j  )
if __name__ == '__main__':
    main()

你可能感兴趣的:(Python爬虫)