内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

用Python爬取網(wǎng)頁數(shù)據(jù)的方法-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務器提供商,新人活動買多久送多久,劃算不套路!

為忻府等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及忻府網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為網(wǎng)站制作、網(wǎng)站設計、忻府網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

小編給大家分享一下用Python爬取網(wǎng)頁數(shù)據(jù)的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

使用Python爬取網(wǎng)頁數(shù)據(jù)的方法:

一、利用webbrowser.open()打開一個網(wǎng)站:

>>> import webbrowser 
>>> webbrowser.open('http://i.firefoxchina.cn/?from=worldindex') 
True

1.從sys.argv讀取命令行參數(shù):打開一個新的文件編輯器窗口,輸入下面的代碼,將其保存為map.py。

2.讀取剪貼板內容:

3.調用webbrowser.open()函數(shù)打開外部瀏覽:

#! python3 
import webbrowser, sys, pyperclip 
if len(sys.argv) > 1: 
 mapAddress = ''.join(sys.argv[1:]) 
else: 
 mapAddress = pyperclip.paste() 
webbrowser.open('http://map.baidu.com/?newmap=1&ie=utf-8&s=s%26wd%3D' + mapAddress

二、用requests模塊從Web下載文件:requests模塊不是Python自帶的,通過命令行運行pip install request安裝。

>>> import requests 
>>> res = requests.get('http://i.firefoxchina.cn/?from=worldindex') #向get中傳入一個網(wǎng)址 
>>> type(res) #響應對象 
<class 'requests.models.Response'> 
>>> print(res.status_code) #響應碼 
200
>>> res.text #返回的文本

在下載文件的過程中,用raise_for_status()方法可以確保下載確實成功,然后再讓程序繼續(xù)做其他事情。

import requests 
res = requests.get('http://i.firefoxchina.cn/?from=worldindex') 
try: 
 res.raise_for_status() 
except Exception as exc: 
 print('There was a problem: %s' % (exc))

三、將下載的文件保存到本地:

>>> import requests 
>>> res = requests.get('http://tech.firefox.sina.com/17/0820/10/6DKQALVRW5JHGE1I.html##0-tsina-1-13074-397232819ff9a47a7b7e80a40613cfe1') 
>>> res.raise_for_status() 
>>> file = open('1.txt', 'wb') #以寫二進制模式打開文件,目的是保存文本中的“Unicode編碼” 
>>> for word in res.iter_content(100000): #<span class="fontstyle0"><span class="fontstyle0">iter_content()</span><span class="fontstyle1">方法在循環(huán)的每次迭代中返回一段</span><span class="fontstyle0">bytes</span><span class="fontstyle1">數(shù)據(jù)</span><span class="fontstyle1">類型的內容,你需要指定其包含的字節(jié)數(shù)</span></span> 
 file.write(word) 
  
  
16997
>>> file.close()

四、用BeautifulSoup模塊解析HTML:在命令行中用pip install beautifulsoup4安裝它。
1.bs4.BeautifulSoup()函數(shù)可以解析HTML網(wǎng)站鏈接requests.get(),也可以解析本地保存的HTML文件,直接open()一個本地HTML頁面。

>>> import requests, bs4 
>>> res = requests.get('http://i.firefoxchina.cn/?from=worldindex') 
>>> res.raise_for_status() 
>>> soup = bs4.BeautifulSoup(res.text) 
  
Warning (from warnings module): 
 File "C:\Users\King\AppData\Local\Programs\Python\Python36-32\lib\site-packages\beautifulsoup4-4.6.0-py3.6.egg\bs4\__init__.py", line 181
 markup_type=markup_type)) 
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently. 
  
The code that caused this warning is on line 1 of the file <string>. To get rid of this warning, change code that looks like this: 
  
 BeautifulSoup(YOUR_MARKUP}) 
  
to this: 
  
 BeautifulSoup(YOUR_MARKUP, "html.parser") 
  
>>> soup = bs4.BeautifulSoup(res.text, 'html.parser') 
>>> type(soup) 
<class 'bs4.BeautifulSoup'>

我這里有錯誤提示,所以加了第二個參數(shù)。

>>> import bs4 
>>> html = open('C:\\Users\\King\\Desktop\\1.htm') 
>>> exampleSoup = bs4.BeautifulSoup(html) 
>>> exampleSoup = bs4.BeautifulSoup(html, 'html.parser') 
>>> type(exampleSoup) 
<class 'bs4.BeautifulSoup'>

2.用select()方法尋找元素:需傳入一個字符串作為CSS“選擇器”來取得Web頁面相應元素,例如:
soup.select('div'):所有名為<div>的元素;

soup.select('#author'):帶有id屬性為author的元素;

soup.select('.notice'):所有使用CSS class屬性名為notice的元素;

soup.select('div span'):所有在<div>元素之內的<span>元素;

soup.select('input[name]'):所有名為<input>并有一個name屬性,其值無所謂的元素;

soup.select('input[type="button"]'):所有名為<input>并有一個type屬性,其值為button的元素。

>>> import requests, bs4 
>>> res = requests.get('http://i.firefoxchina.cn/?from=worldindex') 
>>> res.raise_for_status() 
>>> soup = bs4.BeautifulSoup(res.text, 'html.parser') 
>>> author = soup.select('#author') 
>>> print(author) 
[] 
>>> type(author) 
<class 'list'> 
>>> link = soup.select('link ') 
>>> print(link) 
[<link href="css/mozMainStyle-min.css?v=20170705" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>, <link href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" moz-skin" rel="stylesheet" type="text/css"/>, <link href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" moz-dir" rel="stylesheet" type="text/css"/>, <link href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" moz-ver" rel="stylesheet" type="text/css"/>] 
>>> type(link) 
<class 'list'> 
>>> len(link) 
4
>>> type(link[0]) 
<class 'bs4.element.Tag'> 
>>> link[0] 
<link href="css/mozMainStyle-min.css?v=20170705" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/> 
>>> link[0].attrs 
{'rel': ['stylesheet'], 'type': 'text/css', 'href': 'css/mozMainStyle-min.css?v=20170705'}

 3.通過元素的屬性獲取數(shù)據(jù):接著上面的代碼寫。

>>> link[0].get('href') 
'css/mozMainStyle-min.css?v=20170705

看完了這篇文章,相信你對用Python爬取網(wǎng)頁數(shù)據(jù)的方法有了一定的了解,想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)-成都網(wǎng)站建設公司行業(yè)資訊頻道,感謝各位的閱讀!

分享標題:用Python爬取網(wǎng)頁數(shù)據(jù)的方法-創(chuàng)新互聯(lián)
鏈接分享:http://www.rwnh.cn/article8/pjhop.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司虛擬主機、網(wǎng)站改版、網(wǎng)站策劃、網(wǎng)站收錄、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化
宁都县| 陈巴尔虎旗| 四平市| 从化市| 绥宁县| 全州县| 平罗县| 襄汾县| 天柱县| 开封市| 松滋市| 江达县| 美姑县| 织金县| 杭州市| 嘉荫县| 长岭县| 汉中市| 嘉定区| 越西县| 佳木斯市| 大港区| 南丹县| 萨嘎县| 潮州市| 晋宁县| 厦门市| 太谷县| 收藏| 图们市| 吉木乃县| 淳安县| 巴林左旗| 潮州市| 金华市| 古田县| 江油市| 三门峡市| 威信县| 慈溪市| 黄浦区|