zoukankan      html  css  js  c++  java
  • 爬取信件信息(更新)

     1 import requests
     2 from bs4 import BeautifulSoup
     3 from selenium import webdriver
     4 
     5 
     6 
     7 url='http://www.beijing.gov.cn/hudong/hdjl/com.web.search.mailList.flow'
     8 headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0'}
     9 
    10 resp = requests.post(url, headers=headers)
    11 #print(resp)
    12 resp.encoding = resp.apparent_encoding  # 将网页返回的字符集类型设置为 自动判断的字符集类型
    13 html = resp.text
    14 # 解析成为beautiful对象
    15 soup = BeautifulSoup(html, 'html.parser')
    16 # print(soup)
    17 result = soup.find('body').find('div', {'class': 'conouter'}).find('div', {'id': 'mailul'}).find_all('div', {
    18         'class': 'row clearfix my-2 list-group o-border-bottom2 p-3'})
    19 # print(result)
    20 for info in result:
    21      leixing = info.find('a').find('font').get_text()
    22      biaoti = info.find('a').find('span').get_text()
    23      shijian = info.find('div', {'class': 'o-font2 col-md-3 col-xs-12 text-muted'}).get_text()
    24      bumen = info.find('div', {'class': 'o-font2 col-md-5 col-xs-12 text-muted'}).get_text()
    25      str=('{} {} {} {}'.format(leixing[2:4],biaoti,shijian[5:],bumen[6:]))
    26      list=str.split()#分割空格
    27      #print(list)
    28 #以字典格式输出
    29      dict={'leixing':'','biaoti':'','shijian':'','bumen':''}
    30      dict['leixing']=list[0]
    31      dict['biaoti'] = list[1]
    32      dict['shijian'] = list[2]
    33      dict['bumen'] = list[3]
    34      print(dict)
    35 page=soup.find('body').find_all('div',{'id':'pageDiv1'})
    36 print(page)

    运行结果:

     现在只能爬一页,明天考虑翻页爬取,并提交到mysql数据库

  • 相关阅读:
    BibTex (.bib) 文件的凝视
    SQL注入原理解说,非常不错!
    怎样将文件隐藏在图片中
    白话经典算法系列之五 归并排序的实现
    帮你理解多线程
    很好的理解遗传算法的样例
    薏米红豆粥功效及做法介绍
    Linux makefile 教程 很具体,且易懂
    站点权重对于站点的重要性
    Codeforces Round #250 (Div. 2)——The Child and Set
  • 原文地址:https://www.cnblogs.com/qq1793033075/p/12305420.html
Copyright © 2011-2022 走看看