Python使用bs4库爬虫实例

  • 实 验 内 容 : http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html
    网址中提取大学的排名信 , 包括 排名、学校名称、省市、总分以及所 有的指标得分(生源质量(新生高考成绩得分、培养结果(毕业生就 业率)、社会声誉(社会捐赠收入· 千元)、科研规模(论文数量 · 篇)、 科研质量(论文质量·FWCI )、顶尖成果(高被引论文 · 篇)、顶尖人 才(高被引学者· 人)、)科技服务(企业科研经费 · 千元)、成果转化 (技术转让收入· 千元)、学生国际化(留学生比例)) ,并将爬取的信 息存在当前目录中的“ 大学排名 .csv”
    import re,requests
    import csv
    import numpy
    import lxml
    from bs4 import BeautifulSoup
    
    url1 = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html"
    html1 = requests.get(url1).content.decode()
    
    soup = BeautifulSoup(html1,'lxml')
    tag = soup.find(class_='table table-small-font table-bordered table-striped')
    text1 = tag.find_all('th')[0:4]
    text2 = tag.find_all('option')
    text3 = tag.find_all('td')
    
    th = []
    td = []
    for a in text1+text2:
        th += [a.string]
    for a in text3:
        td += [a.string]
    td = numpy.array(td).reshape(int(len(text3)/14),14)
    
    with open('大学排名.csv','w',newline='',encoding='utf-8') as f:
        writer = csv.writer(f)
        #writer.writeheader()
        writer.writerow(th)
        for a in td:
            print(a)
            writer.writerow(a)
    • 实验内容: https://www.dxsbb.com/news/5463.html 网址中提取大学的排名信息, 包括 排名、学校名称、综合总分、星级排名以及办学层次信 息,并将爬取的信息存在当前目录中的“ 大学排名校友会版 .csv”
      import re,requests
      import csv
      import numpy
      import lxml
      from bs4 import BeautifulSoup
      
      url1 = "https://www.dxsbb.com/news/5463.html"
      html1 = requests.get(url1).content.decode('gbk')
      
      soup = BeautifulSoup(html1,'html.parser')
      text1 = soup.find_all('tbody')[1].find_all('td',)
      
      td = []
      for a in text1:
          td += [a.text]
      td = numpy.array(td).reshape(int(len(text1)/5),5)
      
      with open('大学排名校友会版.csv','w',newline='',encoding='utf-8') as f:
          writer = csv.writer(f)
          #writer.writeheader()
          for a in td:
              print(a)
              writer.writerow(a)
      
      
    • 实验内容:
      1 )打开网址 http://dianying.2345.com/list/----2019---.html ,点击网页 底端的下一页,查看网页 URL 链接的变化;
      2 )爬取第一页网页的中所有电影的 < 名称 > < 演员 > 以及 < 得分 >
      3 )利用步骤二的模式编写函数, 利用循环结构爬取所有页面中电影的信息。
      4 )将爬取的信息存入 最新电影信息 .csv” 文件中。
      import re,requests
      import csv
      import numpy
      import lxml
      from bs4 import BeautifulSoup
      
      film_list = []
      for i in range(1,30):
          url = "http://dianying.2345.com/list/----2019---" + str(i) + ".html"
          html = requests.get(url).text
          soup = BeautifulSoup(html, 'html.parser')
      
          filename_tag = soup.find_all('em', class_='emTit')
          score_tag = soup.find_all('span', {"class": "pRightBottom"})
          star_tag = soup.find_all('span', {"class": "sDes"})
      
          for i in range(0, len(filename_tag)):
              tag = star_tag[i]
              if (tag.em != None):
                  temp = tag.text.strip().split(":")[1].split("\xa0\xa0\xa0")
              else:
                  temp = ['无']
              film_list += [[filename_tag[i].text, score_tag[i].em.text] + temp]
      
      with open('电影.csv', 'w', newline='', encoding='utf-8') as f:
          writer = csv.writer(f)
          #writer.writerow(["名称","评分","主演"])
          for a in film_list:
              print(a)
              writer.writerow(a)
    •  实验内容:
      1 http://www.zhcw.com/ssq/kaijiangshuju/index.shtml?type=0 , 打 开此网址,并通过浏览器中“ 检查 选项发现此网页数据来源规 律;
      2 )爬取 1-150 页的中所有中奖的 < 开奖时间 > < 期号 > < 中奖号 码> < 销售额 > 、、 < 一等奖 > 、、 < 二等奖 > 信息存储至 CSV 文件。
      import re,requests
      import csv
      from bs4 import BeautifulSoup
      
      form = []
      for i in range(1,2):
          url1 = "http://kaijiang.zhcw.com/zhcw/html/ssq/list_%s.html" %(i)
          html1 = requests.get(url1).text
          soup = BeautifulSoup(html1, 'html.parser')
          tag = soup.find_all('tr')
          print(tag)
          for a in tag[2:len(tag) - 1]:
              temp = []
              for b in a.contents[0:12]:
                  if (b != '\n'):
                      temp += [b.text.strip().replace('\r\n', '').replace(' ', '').replace('\n', ' ')]
              form.append(temp)
      
      with open('双色球中奖信息.csv','w',newline='',encoding='utf-8') as f:
          writer = csv.writer(f)
          writer.writerow(['开奖日期', '期号', '中奖号码', '销售额(元)', '一等奖', '二等奖'])
          for a in form:
              print(a)
              writer.writerow(a)
      

     

你可能感兴趣的:(Python爬虫)