当前位置:网站首页>Crawling exercise: Notice of crawling Henan Agricultural University
Crawling exercise: Notice of crawling Henan Agricultural University
2022-07-06 07:07:00 【Rong AI holiday】
Notice of Henan Agricultural University
Introduction to reptiles
With the rapid development of network , The world wide web has become the carrier of a lot of information , How to effectively extract and use this information has become a huge challenge . Search engine (Search Engine), For example, the traditional general search engine AltaVista, Baidu ,Yahoo! and Google etc. , As a tool to assist people in retrieving information, it has become an entrance and guide for users to access the World Wide Web . however , These general search engines also have some limitations , Such as :
(1) Different fields 、 Users with different backgrounds often have different retrieval purposes and requirements , The results returned by general search engines contain a large number of web pages that users don't care about .
(2) The goal of general search engines is to maximize the network coverage , The contradiction between limited search engine server resources and unlimited network data resources will be further deepened .
(3) The rich data form of the world wide web and the continuous development of network technology , picture 、 database 、 Audio / Video multimedia and other different data appear in large quantities , General search engines are often powerless to these data with dense information content and certain structure , Can't find and get .
(4) General search engines mostly provide keyword based search , It is difficult to support queries based on semantic information .
In order to solve the above problems , Focused crawlers, which can capture relevant web resources directionally, emerge as the times require . Focus crawler is a program that automatically downloads Web pages , It grabs the target according to the set target , Selective access to web pages and related links on the world wide web , Get the information you need . With universal crawlers (generalpurpose web crawler) Different , Focused crawlers don't seek big coverage , The goal is to capture the web pages related to a particular topic , Preparing data resources for topic oriented user queries .
Crawl Henan Agricultural University notice announcement
Crawling purpose
It's school season , I want to pay attention to the dynamic of the school . See if there is any information about the beginning of school .
Learn the code , The program completes the notice and announcement of Henan Agricultural University
https://www.henau.edu.cn/news/xwgg/index.shtml Data capture and storage
Analysis steps
1. Import library
import requests
from bs4 import BeautifulSoup
import csv
from tqdm import tqdm
2. Analyze the web
Compare the following links
https://www.henau.edu.cn/news/xwgg/index.shtml
https://www.henau.edu.cn/news/xwgg/index_2.shtml
https://www.henau.edu.cn/news/xwgg/index_3.shtml
It is obvious that the law , In this way, you can use the cycle to crawl
3. Single page analysis
It can be seen that the general structure is consistent , Just select .news_list The information in
4. Save the file
with open(' Notice notice 1.csv', 'w', newline='', encoding='utf-8-sig') as file:
fileWriter = csv.writer(file)
fileWriter.writerow([' date ',' title '])
fileWriter.writerows(info_list)
- with open(‘ Notice notice 1.csv’, ‘w’, newline=’’, encoding=‘utf-8-sig’) as file: This sentence must be written like this to prevent garbled code
Crawling results show
Code
Part of the code explains
t = li.find('span')
if t :
date = t.string
t = li.find('a')
if t :
title = t.string
Be sure to use find, Because there's only one span Elements , Out-of-service find_all function , In addition, judge t If there is a value , Otherwise there will be problems
for i in tqdm(range(1,5)):
if i==1 :
url = base_url + "index.shtml"
else:
str = "index_%s.shtml" % (i)
url = base_url + str
The core code for paging crawling
All the code
import requests
from bs4 import BeautifulSoup
import csv
from tqdm import tqdm
def get_page(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
resp = requests.get(url,headers)
resp.encoding = resp.status_code
resp.encoding = resp.apparent_encoding
#resp.encoding = 'utf-8'
page_text = resp.text
r = BeautifulSoup(page_text,'lxml')
lists = r.select('.news_list ul li')
#print(lists)
info_list_page = []
for li in lists:
t = li.find('span')
if t :
date = t.string
t = li.find('a')
if t :
title = t.string
info = [date,title]
info_list_page.append(info)
return info_list_page
def main():
info_list = []
base_url = "https://www.henau.edu.cn/news/xwgg/"
print(" Topic crawling ...........")
for i in tqdm(range(1,5)):
if i==1 :
url = base_url + "index.shtml"
else:
str = "index_%s.shtml" % (i)
url = base_url + str
#print()
info_page = get_page(url)
#print(info_page)
info_list+=info_page
print(" Climb to success !!!!!!!!!!")
with open(' Notice notice 1.csv', 'w', newline='', encoding='utf-8-sig') as file:
fileWriter = csv.writer(file)
fileWriter.writerow([' date ',' title '])
fileWriter.writerows(info_list)
if __name__ == "__main__" :
main()
If this article is helpful to my friends , I hope you can give me some praise and support ~ Thank you very much. ~
边栏推荐
- L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
- Misc of BUU (update from time to time)
- A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
- 配置树莓派接入网络
- GET 和 POST 请求类型的区别
- win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
- JDBC学习笔记
- Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
- TS Basics
- Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
猜你喜欢
Thought map of data warehouse construction
医疗软件检测机构怎么找,一航软件测评是专家
RichView TRVStyle 模板样式的设置与使用
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
1189. Maximum number of "balloons"
【每日一题】729. 我的日程安排表 I
18.多级页表与快表
UWA Pipeline 2.2.1 版本更新说明
WPF之MVVM
[daily question] 729 My schedule I
随机推荐
基于PyTorch和Fast RCNN快速实现目标识别
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
AI on the cloud makes earth science research easier
Apache dolphin scheduler source code analysis (super detailed)
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
升级版手机检测微信工具小程序源码-支持多种流量主模式
LeetCode 78:子集
Arduino tutorial - Simon games
Cif10 actual combat (resnet18)
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
TS Basics
GET 和 POST 请求类型的区别
UWA Pipeline 2.2.1 版本更新说明
顶测分享:想转行,这些问题一定要考虑清楚!
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例