python 抓取網頁鏈接_從Python中的網頁抓取鏈接

python 抓取網頁鏈接

Prerequisite:

先決條件:

  1. Urllib3: It is a powerful, sanity-friendly HTTP client for Python with having many features like thread safety, client-side SSL/TSL verification, connection pooling, file uploading with multipart encoding, etc.

    Urllib3 :這是一個功能強大,對環境友好的Python HTTP客戶端,具有許多功能,例如線程安全,客戶端SSL / TSL驗證,連接池,使用多部分編碼的文件上傳等。

    Installing urllib3:

    安裝urllib3:

        $ pip install urllib3
    
    
  2. BeautifulSoup: It is a Python library that is used to scrape/get information from the webpages, XML files i.e. for pulling data out of HTML and XML files.

    BeautifulSoup :這是一個Python庫,用于從網頁,XML文件中抓取/獲取信息,即從HTML和XML文件中提取數據。

    Installing BeautifulSoup:

    安裝BeautifulSoup:

        $ pip install beautifulsoup4
    
    

Commands Used:

使用的命令:

html= urllib.request.urlopen(url).read(): Opens the URL and reads the whole blob with newlines at the end and it all comes into one big string.

html = urllib.request.urlopen(url).read() :打開URL并以換行符結尾讀取整個blob,所有這些都變成一個大字符串。

soup= BeautifulSoup(html,'html.parser'): Using BeautifulSoup to parse the string BeautifulSoup converts the string and it just takes the whole file and uses the HTML parser, and we get back an object.

soup = BeautifulSoup(html,'html.parser') :使用BeautifulSoup解析字符串BeautifulSoup轉換該字符串,它只獲取整個文件并使用HTML解析器,然后返回一個對象。

tags= soup('a'): To get the list of all the anchor tags.

tags =湯('a') :獲取所有錨標簽的列表。

tag.get('href',None): Extract and get the data from the href.

tag.get('href',None) :從href中提取并獲取數據。

網頁鏈接的Python程序 (Python program to Links from a Webpage)

# import statements
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
# Get links
# URL of a WebPage
url = input("Enter URL: ") 
# Open the URL and read the whole page
html = urllib.request.urlopen(url).read()
# Parse the string
soup = BeautifulSoup(html, 'html.parser')
# Retrieve all of the anchor tags
# Returns a list of all the links
tags = soup('a')
#Prints all the links in the list tags
for tag in tags: 
# Get the data from href key
print(tag.get('href', None), end = "\n")

Output:

輸出:

Enter URL: https://www.google.com/
https://www.google.com/imghp?hl=en&tab=wi
https://maps.google.com/maps?hl=en&tab=wl
https://play.google.com/?hl=en&tab=w8
https://www.youtube.com/?gl=US&tab=w1
https://news.google.com/nwshp?hl=en&tab=wn
https://mail.google.com/mail/?tab=wmhttps://drive.google.com/?tab=wo
https://www.google.com/intl/en/about/products?tab=wh
http://www.google.com/history/optout?hl=en
/preferences?hl=en
https://accounts.google.com/ServiceLogin?hl=en&passive=true
&continue=https://www.google.com/
/advanced_search?hl=en&authuser=0
/intl/en/ads/
/services/
/intl/en/about.html
/intl/en/policies/privacy/
/intl/en/policies/terms/

翻譯自: https://www.includehelp.com/python/scraping-links-from-a-webpage.aspx

python 抓取網頁鏈接

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377869.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377869.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377869.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

四、模擬英語四六級答題卡識別閱卷評分

一、思路分析 首先拿到答題卡照片的時候,需要對照片進行一系列預處理操作,通過透視變換將圖像擺正方便后續的操作。每一道題五個選項,有五道題,通過字典存放準確答案。沒有依次對答題卡進行輪廓檢測,這里采用的是正方…

leetcode 17. 電話號碼的字母組合 思考分析

題目 給定一個僅包含數字 2-9 的字符串,返回所有它能表示的字母組合。 給出數字到字母的映射如下(與電話按鍵相同)。注意 1 不對應任何字母。 思考與遞歸程序 解空間樹的寬度是輸入數字對應的字符的個數,深度是輸入的數字的個數…

Blockquotes,引用,html里面,經常用到的一個!

blockquote元素的使用已經非常多樣化&#xff0c;但語義上它只適用于一件事–標記了一段你的網頁被引用從另一來源。這意味著&#xff0c;如果你想讓那些花俏的引文&#xff0c;<blockquote>是不是你應該使用元素。讓我們看一看如何你應該使用此元素&#xff1a; <art…

仔細分析了下這7行,貌似時間復雜度,空間復雜度都不大,為嘛就是執行效率這么低?...

for(Girl girl Girls.first(); !myGirlFriend.like(me); girl Girls.next()){if(!girl.hasBoyFriend(now) && i.like(girl)) { GirlFriend myGirlFriend (GirlFriend)girl; }} 轉載于:https://www.cnblogs.com/naran/archive/2011/12/28/2305467.html…

BHMS的完整形式是什么?

BHMS&#xff1a;順勢療法醫學和外科學士 (BHMS: Bachelor of Homeopathic Medicine and Surgery) BHMS is an abbreviation of Bachelor of Homeopathic Medicine and Surgery. It is a medical degree program for under graduation in Homeopathy; an alternative move towa…

c++編程思想2 --友元存儲控制

友元friend在c中的應用 我們知道在c的類訪問權限中,private和 protected在類外面進行訪問的時候 會因為權限而不能訪問 &#xff0c;友元就解決了這個問題 。 可以這樣理解&#xff0c;他為外部的 函數 或者類 進行了 訪問授權,其實這已經超出OOP的范疇,但是對于C而言是以實用…

WordPress Event Easy Calendar插件多個跨站請求偽造漏洞

漏洞名稱&#xff1a;WordPress Event Easy Calendar插件多個跨站請求偽造漏洞CNNVD編號&#xff1a;CNNVD-201309-083發布時間&#xff1a;2013-09-11更新時間&#xff1a;2013-09-11危害等級&#xff1a; 漏洞類型&#xff1a;跨站請求偽造威脅類型&#xff1a;遠程CVE編號&…

XML轉txt格式腳本

一、東北大學老師收集的鋼材缺陷數據集是XML格式的&#xff0c;但是YOLOv5只允許使用txt文件標簽 例如其中一種缺陷圖片所對應的標簽&#xff1a;crazing_1.xml <annotation><folder>cr</folder><filename>crazing_1.jpg</filename><source&…

python程序生成exe_使用Python程序生成QR代碼的Python程序

python程序生成exeQR code is a short form of the quick response code. It is a type of matrix barcode that contains some information like some specific link, important message, email-id, etc. In Python, the qrcode module is used to generate the QR code of so…

leetcode 242. 有效的字母異位詞 思考分析

題目 給定兩個字符串 s 和 t &#xff0c;編寫一個函數來判斷 t 是否是 s 的字母異位詞。 我們先考慮低階版本&#xff0c;認為字符只有26種可能&#xff0c;然后將a ~ z的字符映射到數組的索引0 ~ 25&#xff0c;數組中存放的則是該索引出現的頻次。 記錄下s的頻次和t的頻次…

總結一下ERP .NET程序員必須掌握的.NET技術,掌握了這些技術工作起來才得心應手...

從畢業做.NET到現在&#xff0c;有好幾年了&#xff0c;自認為只能是達到熟練的水平&#xff0c;談不上精通。所以&#xff0c;總結一下&#xff0c;自己到底熟練掌握了哪些.NET方面的開發技術&#xff0c;以此對照&#xff0c;看看還有哪些不足&#xff0c;歡迎補充。 1 .NET …

js \n直接顯示字符串_顯示N個字符的最短時間

js \n直接顯示字符串Problem statement: 問題陳述&#xff1a; You need to display N similar characters on a screen. You are allowed to do three types of operation each time. 您需要在屏幕上顯示N個相似的字符。 每次允許您執行三種類型的操作。 You can insert a c…

示例 Demo 工程和 API 參考鏈接

Camera Explorer&#xff1a;有關 Windows Phone8 中有關增強 Camera API 的使用。文章鏈接 Filter Effects&#xff1a;對拍攝的照片或者圖片庫中的照片應用 Nokia Imaging SDK 中的濾鏡。文章鏈接 Filter Explorer&#xff1a;演示了對新拍攝圖片或者現有圖片的編輯功能&…

三、標簽準備

所有操作均在anaconda中的自己配置的環境下進行 一、安裝labelimg 因為YOLO模型所需要的樣本標簽必須是txt類型&#xff0c;本人使用labelimg軟件進行對圖像進行打標簽操作。 pip install pycocotools-windows pip install pyqt5 pip install labelimg 通過labelimg命令打…

ubuntu 8.04安裝應用軟件Can't find X includes錯誤解決辦法

系統很小。應用軟件都的自己裝。 首先把 APT’s database is not updated. # apt-get update    # apt-get upgrade 再裝其它軟件。 make xconfigure 無法運行時&#xff1a; apt-get install qt3-dev-tools 編譯QVFB  是出現&#xff1a; 出現&#xff1a;C preproces…

leetcode 39. 組合總和 思考分析

目錄1、題目2、思考分析3、未經優化代碼4、剪枝優化1、題目 給定一個無重復元素的數組 candidates 和一個目標數 target &#xff0c;找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重復被選取。 2、思考分析 解空間樹寬度部分即數…

java uuid靜態方法_Java UUID equals()方法與示例

java uuid靜態方法UUID類equals()方法 (UUID Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this object equals to the given object or not. equals()方法用于檢…

一、機器學習概念

一、何為機器學習(Mechine Learning)&#xff1f; 答&#xff1a;利用已有數據(經驗)&#xff0c;來訓練某種模型&#xff0c;利用此模型來預測未來。機器學習是人工智能的核心Mechine Learning。 例如&#xff1a;你和狗蛋兒7點在老槐樹下集合&#xff0c;如何一塊約去開黑&a…

Java線程新特征——Java并發庫

一、線程池 Sun在Java5中&#xff0c;對Java線程的類庫做了大量的擴展&#xff0c;其中線程池就是Java5的新特征之一&#xff0c;除了線程池之外&#xff0c;還有很多多線程相關的內容&#xff0c;為多線程的編程帶來了極大便利。為了編寫高效穩定可靠的多線程程序&#xff0c;…

第一篇博文

剛剛申請博客&#xff0c;開通了&#xff0c;很高興。但是由于這幾天考試比較多&#xff0c;等考完之后&#xff0c;再開始正式寫博客&#xff0c;與諸君共進步&#xff01; 2012/1/1 18:20 轉載于:https://www.cnblogs.com/zhenglichina/archive/2012/01/01/2309561.html