Python笔记-获取某百科页面所有URL(提取某百科所有URL)
生活随笔
收集整理的這篇文章主要介紹了
Python笔记-获取某百科页面所有URL(提取某百科所有URL)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
程序運(yùn)行截圖如下:
這里分析下頁(yè)面:
?
?凡是百度百科的都是在此url上
https://baike.baidu.com/item/xxxxx,所以可以直接提取。
這里我們用個(gè)隊(duì)列,將這個(gè)頁(yè)面的所有有關(guān)的url入隊(duì),然后出隊(duì)列,進(jìn)行訪(fǎng)問(wèn):
還有個(gè)要注意的,要偽造成瀏覽器,不然會(huì)回?cái)?shù)據(jù)
import requests import queue import time from bs4 import BeautifulSoupheader = {'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9','Accept-Language' : 'zh-CN,zh;q=0.9','Cache-Control' : 'no-cache','Connection' : 'keep-alive','Cookie' : 'xxxxxxx','Host' : 'baike.baidu.com','Pragma' : 'no-cache','User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36','sec-ch-ua' : '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"' }baseUrl = "https://baike.baidu.com/item/" urlQueue = queue.Queue(10000)def getRequest(url):response = requests.get(url, headers = header)return response.textpassif __name__ == '__main__':urlQueue.put('%E7%BB%9F%E8%AE%A1%E5%AD%A6/1175')for i in range(100):url = urlQueue.get()content = getRequest(baseUrl + url)contentSoup = BeautifulSoup(content, "html.parser")urlAllList = contentSoup.select("a")for urlTmp in urlAllList:if urlTmp.attrs.__contains__('href'):urlString = urlTmp['href']if '/item' in urlString:testUrl = urlString.split('/item/')[1]urlQueue.put(testUrl)passpasspassprint('over')pass總結(jié)
以上是生活随笔為你收集整理的Python笔记-获取某百科页面所有URL(提取某百科所有URL)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: canvas笔记-使用arc与lineT
- 下一篇: PHP笔记-学生成绩例子