Python实验项目9 :网络爬虫与自动化

实验 1:爬取网页中的数据。

要求:使用 urllib 库和 requests 库分别爬取 http://www.sohu.com 首页的前 360 个字节的数据。

# 要求:使用 urllib 库和 requests 库分别爬取 http://www.sohu.com 首页的前 360 个字节的数据。
import urllib.request
import requests
# 使用 urllib 库爬取 http://www.sohu.com 首页的前 360 个字节的数据。
url = 'http://www.sohu.com'
req = urllib.request.Request(url)
res = urllib.request.urlopen(req)
data = res.read(360)
print(data)


# 使用 requests 库爬取 http://www.sohu.com 首页的前 360 个字节的数据。
#url = 'http://www.sohu.com'
#res = requests.get(url)
#data = res.content[:360]
#print(data)

实验 2:测试 BeautifulSoup 对象的方法。

要求:

1)创建 BeautifulSoup 对象。
2)测试搜索文档树的 find_all()方法和 find()方法。
# 实验 2:测试 BeautifulSoup 对象的方法。
# 要求:
# 1)创建 BeautifulSoup 对象。
# 2)测试搜索文档树的 find_all()方法和 find()方法。
from bs4 import BeautifulSoup
import requests
# 过http请求加载网页
response = requests.get("http://www.sohu.com")
# 创建BeautifulSoup对象
soup = BeautifulSoup(response.text, "html.parser")
# 搜索文档树的find_all()方法
print(soup.find_all("a"))
# 搜索文档树的find()方法
print(soup.find("a"))

 实验 3:爬取并分析网页页面数据。

 (1)使用requests库爬取https://www.hnnu.edu.cn/main.htm首页内容。
(2)编写程序获取https://www.hnnu.edu.cn/119/list.htm的通知公告的信息。  
# 实验 3:爬取并分析网页页面数据。
# (1)使用requests库爬取https://www.hnnu.edu.cn/main.htm首页内容。
# (2)编写程序获取https://www.hnnu.edu.cn/119/list.htm的通知公告的信息。
import requests
from bs4 import BeautifulSoup
url = 'https://www.hnnu.edu.cn/main.htm'
res = requests.get(url)
soup = BeautifulSoup(res.text,'html.parser')
print(soup.find_all('a'))
print(soup.find('a'))

for i in range(1,23,1):
    url = 'https://www.hnnu.edu.cn/119/list.htm{}.htm'.format(i)
    res = requests.get(url)
    soup = BeautifulSoup(res.text,'html.parser')
    print("-------------------------------------------------------")
    print(soup)
    #print(soup.find('a'))

实验四:爬取关键词+制作词云

# 爬取关键字
from typing import List, Any

import requests

from bs4 import BeautifulSoup

urls=[]

for i in range(1,23):

    urls.append('https://www.hnnu.edu.cn/gyxy/list'+str(i)+'.htm')# 这里改地址

def getURL(url):

    r=requests.get(url)

    r.encoding='utf-8'

    return r.text

def getSoup(url):

    txt=getURL(url)

    soup=BeautifulSoup(txt,'html.parser')

    return soup



def getContent(soup):

    content=soup.find('ul',{'class':'news_list list2'})# ul对应的list

    titles=[]

    dates=[]

    for item in content.find_all('li'):

#         titles.append(item.a['title'])

        date=item.find('span',{'class':'news_meta'})

#         dates.append(date.string)

        titles.append([date.string,item.a['title']])

    return titles

articles=[]

for url in urls:

    soup=getSoup(url)

    articles.append(getContent(soup))
t=""
for item in articles:
    t=t+(str)(item)
# 制作词云
import jieba

import wordcloud

jieba.add_word("淮南师范学院")

ls=jieba.lcut(t)

w=wordcloud.WordCloud(width=1000,height=700,background_color='white',font_path='simhei.ttf')

txt=" ".join(ls)

w.generate(txt)

w.to_file('out1.png')

  • 13
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值