目錄
一、翻頁分析:
二、代碼邏輯
1、入口程序修改
2、page參數(shù)傳入
三、完整代碼
1、運行結(jié)果
2、錯誤分析:
3、缺陷代碼:
4、完善邏輯:
5、完善代碼:
(說在前面,任何邏輯都有很多方法實現(xiàn),我們先從最笨的講起)
(注:其中的cookie需要填自己的)
一、翻頁分析:
通過對比第一頁與第二頁的URL來看是否有差別
可以發(fā)現(xiàn)通過參數(shù)pageNum=來控制頁面
(第一頁pageNum參數(shù)被省略,如果下一面沒有數(shù)據(jù)了,也可能出錯)
二、代碼邏輯
1、入口程序修改
if __name__ == '__main__':
with open('1.csv', 'a', encoding='utf-8', newline='') as f:
csv_w = csv.writer(f)
csv_w.writerow(('公司名', 'URL', '類型', '資金'))
for page in range(1, 6):
get_TYC_info(page)
print(f'第{page}頁已爬完')
time.sleep(2)
(1)if __name__ == '__main__':
一個條件語句,判斷當(dāng)前模塊是否直接被運行。當(dāng)該模塊直接執(zhí)行時,以下代碼塊將被執(zhí)行。
(2)with open('1.csv', 'a', encoding='utf-8', newline='') as f:
打開名為"1.csv"的文件,并賦值給變量f。使用'a'模式打開文件,表示以追加方式寫入文件內(nèi)容。encoding='utf-8'表示以UTF-8編碼打開文件,newline=''表示在寫入時不插入額外的換行符。
(3)csv_w = csv.writer(f)
創(chuàng)建一個CSV寫入器對象,并將文件對象f傳遞給它。這樣可以通過該寫入器對象來操作CSV文件。
(4)csv_w.writerow(('公司名', 'URL', '類型', '資金'))
使用CSV寫入器對象csv_w將一個包含四個元素的元組寫入CSV文件。這個元組表示CSV文件的表頭,即第一行的內(nèi)容。
(5)for page in range(1, 6):
這是一個循環(huán)語句,從1循環(huán)到5,將每個循環(huán)中的值賦給變量page。
(6)get_TYC_info(page)
調(diào)用名為get_TYC_info的函數(shù),并傳遞當(dāng)前循環(huán)的值page作為參數(shù)。這個函數(shù)用于爬取TYC網(wǎng)站上的信息。
(7)print(f'第{page}頁已爬完')
打印當(dāng)前循環(huán)的值page,并顯示"第X頁已爬完"的消息。這是一個簡單的提示,用于顯示程序的進度。
(8)time.sleep(2)
程序暫停執(zhí)行2秒鐘。這是為了避免過快地請求網(wǎng)頁導(dǎo)致被屏蔽或限制訪問。
2、page參數(shù)傳入
def get_TYC_info(page):
TYC_url = f"https://www.tianyancha.com/search?key=&sessionNo=1688538554.71584711&base=hub&cacheCode=00420100V2020&city=wuhan&pageNum={page}"
1、將page參數(shù)傳入進get_TYC_info()函數(shù)(頁面的爬取函數(shù))
2、f'URL……&pageNum={page}'
將URL中的page參數(shù)動態(tài)修改
三、完整代碼
(代碼在最后)
1、運行結(jié)果
(第1、2面都是可以爬的)
?第2面開始有報錯
(這個錯誤問題我們來分析一下)
其實就是爬取的列表為空,導(dǎo)致的錯誤
2、錯誤分析:
看圖說話錯誤原因:
是不是到我們報錯的位置這,下一個企業(yè)就沒了相關(guān)類型了,對吧
所以爬取到的列表為空,從而導(dǎo)致了不能繼續(xù)執(zhí)行爬取下一級---->所以報錯
3、缺陷代碼:
import time
import requests
from bs4 import BeautifulSoup
import csv
def get_TYC_info(page):
TYC_url = f"https://www.tianyancha.com/search?key=&base=hub&city=wuhan&cacheCode=00420100V2020&sessionNo=1688108233.45545222&pageNum={page}"
html = get_page(TYC_url)
soup = BeautifulSoup(html, 'lxml')
GS_list = soup.find('div', attrs={'class': 'index_list-wrap___axcs'})
GS_items = GS_list.find_all('div', attrs={'class': 'index_search-box__7YVh6'})
for item in GS_items:
title = item.find('div', attrs={'class': 'index_name__qEdWi'}).a.span.text
link = item.a['href']
company_type = item.find('div', attrs={'class': 'index_tag-list__wePh_'}).find_all('div', attrs={'class': 'index_tag-common__edIee'})
type_texts = [element.text for element in company_type]
money = item.find('div', attrs={'class': 'index_info-col__UVcZb index_narrow__QeZfV'}).span.text
print(title.strip(), link, type_texts, money)
def get_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36',
'Cookie':'!!!!!!!!!!'
}
response = requests.get(url, headers=headers, timeout=10)
return response.text
except:
return ""
if __name__ == '__main__':
with open('1.csv', 'a', encoding='utf-8', newline='') as f:
csv_w = csv.writer(f)
csv_w.writerow(('公司名', 'URL', '類型', '資金'))
for page in range(1, 6):
get_TYC_info(page)
print(f'第{page}頁已爬完')
time.sleep(2)
4、完善邏輯:
加上了一個if判斷,第一個爬取點不為none才繼續(xù)往后
if company_type_div is not None:
company_type = company_type_div.find_all('div', attrs={'class': 'index_tag-common__edIee'})
type_texts = [element.text for element in company_type]
else:
type_texts=''
運行結(jié)果:
指定的5面全部爬取完了
5、完善代碼:
(注:其中的cookie需要填自己的)
import time
import requests
from bs4 import BeautifulSoup
import csv
def get_TYC_info(page):
TYC_url = f"https://www.tianyancha.com/search?key=&sessionNo=1688538554.71584711&base=hub&cacheCode=00420100V2020&city=wuhan&pageNum={page}"
html = get_page(TYC_url)
soup = BeautifulSoup(html, 'lxml')
GS_list = soup.find('div', attrs={'class': 'index_list-wrap___axcs'})
GS_items = GS_list.find_all('div', attrs={'class': 'index_search-box__7YVh6'})
for item in GS_items:
title = item.find('div', attrs={'class': 'index_name__qEdWi'}).a.span.text
link = item.a['href']
company_type_div = item.find('div', attrs={'class': 'index_tag-list__wePh_'})
if company_type_div is not None:
company_type = company_type_div.find_all('div', attrs={'class': 'index_tag-common__edIee'})
type_texts = [element.text for element in company_type]
else:
type_texts=''
money = item.find('div', attrs={'class': 'index_info-col__UVcZb index_narrow__QeZfV'}).span.text
print(title.strip(), link, type_texts, money)
def get_page(url):
try:
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36',
'Cookie':'!!!!!!!!!!'
}
response = requests.get(url, headers=headers, timeout=10)
return response.text
except:
return ""
if __name__ == '__main__':
with open('1.csv', 'a', encoding='utf-8', newline='') as f:
csv_w = csv.writer(f)
csv_w.writerow(('公司名', 'URL', '類型', '資金'))
for page in range(1, 6):
get_TYC_info(page)
print(f'第{page}頁已爬完')
time.sleep(2)
網(wǎng)絡(luò)安全小圈子
README.md · 書半生/網(wǎng)絡(luò)安全知識體系-實戰(zhàn)中心 - 碼云 - 開源中國 (gitee.com)https://gitee.com/shubansheng/Treasure_knowledge/blob/master/README.md文章來源:http://www.zghlxwxcb.cn/news/detail-525822.html
GitHub - BLACKxZONE/Treasure_knowledgehttps://github.com/BLACKxZONE/Treasure_knowledge文章來源地址http://www.zghlxwxcb.cn/news/detail-525822.html
到了這里,關(guān)于【網(wǎng)絡(luò)安全帶你練爬蟲-100練】第5練:爬蟲的翻頁操作+錯誤情況過濾的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!