Python3 css选择器实战(二):爬取猫眼电影网

举报
悦来客栈的老板 发表于 2020/12/28 23:59:28 2020/12/28
5.4k+ 0 0
【摘要】 #coding=utf-8import reimport timeimport requestsfrom requests.exceptions import RequestExceptionfrom bs4 import BeautifulSoupfrom prettytable import PrettyTable def getHtml(url): try: head...

      #coding=utf-8
      import re
      import time
      import requests
      from requests.exceptions import RequestException
      from bs4 import BeautifulSoup
      from prettytable import PrettyTable
      def getHtml(url):
      try:
       headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0'}
       response = requests.get(url,headers = headers)
      if response.status_code == 200:
      return response.text
      return None
      except RequestException:
      return None
      if __name__=='__main__':
       lmovie = []
       lactor = []
       ltime = []
       lscore = []
       table = PrettyTable(['NO.','电影','主演','上映时间','猫眼评分'])
       index = 0
      for page in range(0,100,10):
       url = 'http://maoyan.com/board/4?offset={}'.format(page)
       Html = getHtml(url)
      if Html is None:
       contine
       Soup = BeautifulSoup(Html,'lxml')
       names = Soup.select('div.movie-item-info > p:nth-of-type(1)')
      for name in names:
       lmovie.append(name.text)
       actors = Soup.select('div.movie-item-info > p:nth-of-type(2)')
      for actor in actors:
       actor = actor.text
       actor = actor.split(':')[-1]
       lactor.append(actor.strip())
       releasetimes = Soup.select('div.movie-item-info > p:nth-of-type(3)')
      for retime in releasetimes:
       retime = retime.text
       retime = retime.split(':')[-1]
       ltime.append(retime)
       scores = Soup.select('p.score > i:nth-of-type(1)')
      for score in scores:
       lscore.append(score.text)
       frascores = Soup.select('p.score > i:nth-of-type(2)')
      for i in range(len(frascores)):
       lscore[index] += frascores[i].text
       index += 1
       time.sleep(1)
      for i in range(100):
       table.add_row([str(i+1),lmovie[i],lactor[i],ltime[i],lscore[i]])
      print (table)
  
 

总结:用nth-of-type的感觉太爽了,代码也要优美很多。

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/82917075

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。