- 爬取部分豆瓣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)