m0_62895523 2022-12-20 19:53 采纳率: 80%
浏览 176
已结题

python爬虫编程习题

自选一个大学,从大学的主页上爬出机构的URL、名称、简要介绍、电话、邮箱。再进入下属机构的主页,从中爬出下属机构的URL、名称、简要介绍、电话、邮箱。所有爬出的数据保存于同一个文件中。
(建议使用beautifulsoup,等基础入门的,因为是初学者一名)

  • 写回答

6条回答 默认 最新

  • CSDN专家-showbo 2022-12-20 20:05
    关注

    这个倒是同一个模板。。。不过机构和院系比较少,不过研究bs也差不多了

    from bs4 import BeautifulSoup
    import requests
    from openpyxl import Workbook
    header = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
    url = "http://www.bowenedu.cn/xxgk1/xxjj.htm"
    
    data=[]
    html=requests.get(url,headers=header).content.decode('utf-8')
    soup = BeautifulSoup(html,"html.parser")
    name=soup.find('title').text.split('-')[-1]
    intro=soup.find('div',{"id":"vsb_content_4"}).text
    ps=soup.findAll('p')
    phone=""
    email=""
    for p in ps:
        if '电 话:' in p.text:
            phone=p.text.replace('电 话:','')
        if '电子信箱:' in p.text:
            email=p.text.replace('电子信箱:','')
    data.append([url,name,intro,phone,email])
    
    #院部设置
    depts=soup.select('div.menu li.item')[3].select('.secNav a')
    for dept in depts:
        url=dept.get('href')
        if ".asp" not in url:
            continue
        name=dept.text
        html=requests.get(url,headers=header).content.decode('gb2312')
        soup = BeautifulSoup(html,"html.parser")
    
        arr=soup.select('#foot')[0].text.strip().split('    ')
        phone=arr[0].replace('学院咨询电话:','')
        email=arr[1].replace('管理员信箱:','')
    
        introurl='http://tiyu.bowenedu.cn'+str(soup.select('#guide a')[2].get('href'))
    
        html=requests.get(introurl,headers=header).content.decode('gb2312')
        isoup = BeautifulSoup(html,"html.parser")
        intro=isoup.select('div.down')[0].text
    
        data.append([url,name,intro,phone,email])
    
    
    
    wb=Workbook()
    sheet=wb.worksheets[0]#得到默认的工作簿
    sheet.title="兰州博文科技学院"#改名称
    sheet.append(['URL','名称','简要介绍','电话','邮箱'])
    for item in data:
        sheet.append(item)
     
    wb.save("兰州博文科技学院.xlsx")
    print('采集完毕')
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月21日
  • 已采纳回答 12月21日
  • 创建了问题 12月20日

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿