爬虫-协和数据
Day1-獲取協和首頁其中一個子項的所有a標簽
介紹:
樣本信息:協和
處理內容:按索引字符串,遍歷當前頁面及其相關頁面,當頁面中包含索引值時,返回相應的地址。用于對頁面的數據檢索。
part1 帶請求頭爬取數據
import requests from bs4 import BeautifulSoupheader = {"user-agent":"你自己的信息" }res = requests.get('https://www.pumch.cn/index.html',header) html = res.textsoup = BeautifulSoup(html,'html.parser') items=soup.find('ul',class_='links').find_all('li')part2 獲取首頁信息
#獲取協和首頁鏈接信息 def get_home():home = []for item in items:url='https://www.pumch.cn'name = item.find('a').get('href').strip()str_list = list(name)if(name.find(url)< 0):str_list.insert(0, url)str = ''.join(str_list)home.append(str)else:home.append(name)return home #print(get_home())輸出數據:
['https://www.pumch.cn/patient.html', 'https://www.pumch.cn/learning.html', 'https://www.pumch.cn/centenary.html', 'https://www.pumch.cn/staff.html', 'https://www.pumch.cn/en.html']part3 獲取其中一頁的所有a標簽
但在這里獲取出的a標簽并不是完全可以的,還需要進行加工
res_learning = requests.get(get_home()[1],header)html_learning = res_learning.text soup1 = BeautifulSoup(html_learning)tags=soup1.find_all('a') #print(tags) #for tag in tags: # print(tag.get('href'))輸出結果展示特征部分:
/html/index.html?scene_id=51117035 /register.html http://telemedicine.pumch.cn http://paper.pumch.cn/ http://mjpumch.cbpt.cnki.net/WKC3/WebPublication javascript:void(0); javascript:; # None ......part4 數據加工
處理規則:
1.帶javascript的數據、None的數據、#的不要,
2.以http:開頭的保留
3.字符串中不包含https://www.pumch.cn的數據,且不滿足以兩個條件的在首位置插入https://www.pumch.cn
輸出結果:
https://www.pumch.cn/register.html https://www.pumch.cn/visitinfo.html https://www.pumch.cn/reportquery.html https://www.pumch.cn/centenary.html https://www.pumch.cn/learning.html https://www.pumch.cn/research/gudie.html https://www.pumch.cn/research/gudie.html https://www.pumch.cn/single/21556.html https://www.pumch.cn/trend.html https://www.pumch.cn/notice.html ......總結
- 上一篇: RAW,RGB,YUV简单理解
- 下一篇: 计算机课堂热身游戏,简单的两款小游戏,带