Python教程

Python数据分析

本文主要是介绍Python数据分析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

  • 爬取部分豆瓣top250
 1 import requests
 2 from lxml import etree
 3 
 4 
 5 def getData(number):
 6     url = "https://movie.douban.com/top250?start={0}".format(number)
 7     headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'}
 8     response = requests.get(url, timeout=10, headers=headers)
 9     response.encoding = 'utf-8'
10     parser = etree.HTMLParser(encoding='utf-8')
11     html = etree.HTML(response.text, parser=parser)
12     message = html.xpath('//ol[@class="grid_view"]/li/div[@class="item"]/div[@class="pic"]/a/img/@alt')  # 根据网页结构不同,自行调整
13     print(message)
14 
15 
16 if __name__ == '__main__':
17     # 每页25个电影,取前10页
18     for number in range(0, 250, 25):
19         getData(number)

 

这篇关于Python数据分析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!