xpath爬取豆瓣电影top250
import requests from lxml import etreeheaders = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', 'Host': 'movie.douban.com' } def get_one_page(base_url): for i in range(10): url=base_url str(i*25)# response=requests.get(url, headers=headers) html=etree.HTML(response.content) items = html.xpath('//ol/li/div[@class="item"]') for item in items: try: rank=item.xpath('./div[@class="pic"]/em/text()')#提取电影的排名 title=item.xpath('./div[@class="info"]/div[@class="hd"]/a/span/text()')#提取电影名 quote=item.xpath('./div[@class="info"]//p[@class="quote"]/span/text()')#提取quote score=item.xpath('//div[@class="star"]/span[contains(@class,"rating_num")]/text()') if len(quote)==0: quote=[" "] print(rank[0] "\t", title[0] "\t", score[0] "\t", quote[0] "\t", )#注意到xpath返回的都是list。 except : print("出错!") pass if __name__ == '__main__': url = 'https://movie.douban.com/top250?start=' get_one_page(url)