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))

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小瑾比个耶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值