python 网络爬虫

编程实现:利用requests爬虫库和beautifulsoup4解析库实现指定网站文章标题和对应网址爬取。

已知:

(1)爬取网址:信息学院首页

(2)编程实现爬取"计算机科学系"在"信息学院"主页上的网址

from bs4 import BeautifulSoup
import requests
url="https://coi.hzau.edu.cn/index.htm"
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '+\
         'AppleWebkit/537.36 (KHTML, like Gecko)'+\
         'Chrome/99.0.4844.51'+\
         'Safari/537.3'+\
         'Edg/99.0.1150.39'}
res=requests.get(url,headers=headers)
res.encoding='utf-8'
soup=BeautifulSoup(res.text,"lxml")
data=soup.find_all('a')
with open(r'data.txt','w') as f:
    for i in data:
        f.write(i.get('href')+" "+i.text+'\n')
with open(r'data.txt','r') as f:
    a=input("请输入想查找的专业:")
    l=[]
    lines=f.readlines()
    for line in lines:
        if a in line:
            if line not in l:
                print("网址为:","https://coi.hzau.edu.cn/"+line)
            l.append(line)
import requests
from bs4 import BeautifulSoup
url="http://coi.hzau.edu.cn/"
header={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50"}
res=requests.get(url,headers=header)
res.encoding='utf-8'
html=res.text
soup=BeautifulSoup(html,'html.parser')
a=soup.find_all('a')
for i in a:
    i=str(i)
    if("计算机科学" in i):
        i=i.split('"')
        url=i[1]
        break
baseurl="http://coi.hzau.edu.cn/"
print("信息学院计算机科学系的主页网址为:",baseurl+url)
url=baseurl+url
response=requests.get(url,headers=header)
response.encoding='utf-8'
soup=BeautifulSoup(response.text,"html.parser")
a=soup.find_all("a")
tn=[]
for i in a:
    href=str(i)
    if "info" in href:
        href=href.split(">")[1]
        tn.append(href.split("<")[-2])
print("计算机科学系的老师有:",' '.join(tn))

相关推荐

  1. Python----网络爬虫

    2024-07-14 10:46:01       47 阅读
  2. 26.Python 网络爬虫

    2024-07-14 10:46:01       59 阅读
  3. python网络爬虫基础

    2024-07-14 10:46:01       31 阅读
  4. python网络爬虫——Scrapy

    2024-07-14 10:46:01       32 阅读
  5. python实现网络爬虫

    2024-07-14 10:46:01       29 阅读
  6. python教程---网络爬虫

    2024-07-14 10:46:01       28 阅读
  7. python 网络爬虫

    2024-07-14 10:46:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 10:46:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 10:46:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 10:46:01       58 阅读
  4. Python语言-面向对象

    2024-07-14 10:46:01       69 阅读

热门阅读

  1. Writing Bazel rules: simple binary rule

    2024-07-14 10:46:01       19 阅读
  2. UVA12342 Tax Calculator 题解

    2024-07-14 10:46:01       24 阅读
  3. Font Awesome 图表图标

    2024-07-14 10:46:01       27 阅读
  4. c++课后作业

    2024-07-14 10:46:01       24 阅读
  5. k8s开启deopos功能报错总结

    2024-07-14 10:46:01       25 阅读
  6. 【机器学习】ChatGLM2-6B 分词器 Tokenizer 的使用

    2024-07-14 10:46:01       35 阅读