import requestsimport lxml.htmlmyheaders = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"} # 获取豆瓣电影Top250的网页,# 并转换了可使用XPath分析的对象http_response = requests.get('/top250', headers=myheaders)http_response.encoding = 'utf-8'html = lxml.html.fromstring(http_response.text)# 获取所有电影列表的<li>元素movies = html.xpath('//*[@id="content"]/div/div[1]/ol/li')for movie in movies:# 获取第一个电影标题所在的元素first_title_element = movie.xpath('.//span[1]')# 获取电影标题title = first_title_element[0].text_content()# 获取电影评分所在的元素rating_element = movie.xpath('.//span[@class="rating_num"][@property="v:average"]')# 获取评分rating = rating_element[0].text_content()# 格式化输出电影标题及评分print('<<{}>> - {}'.format(title, rating))
600字范文,内容丰富有趣,生活中的好帮手!