Python網路爬蟲基礎 一

2021-08-21 12:17:01 字數 1940 閱讀 9342

2. urllib和urllib2模組使用

3. requests模組使用

4. python三種網頁內容抓取方法

當瀏覽器向web伺服器發出請求時,它向伺服器傳遞了乙個資料塊,也就是請求信 息,http請求資訊由3部分組成:

- 請求方法 uri 協議/版本

- 請求頭(request header):

- 請求正文

右擊滑鼠選擇檢查

requests 是用python語言編寫,基於 urllib,採用 apache2 licensed 開源協議的 http 庫。它比 urllib 更加方便,可以節約我們大量的工作,完全滿足 http 測試需求。

conda install requests
或者

#出現亂碼則將encoding改為頁面真正的字元編碼集

print r.text

import request

payload =

r = requests.post(',data=payload)

print r

print r.text

import requests

headers =

html = requests.get("",headers=headers)

html.status_code

html.content

html.text

import requests

proxies =

#requests模擬瀏覽器

response = requests.get("",headers=headers,proxies=proxies)

print(response.status_code)

print(response.content)

from bs4 import beautifulsoup as bs

html = '''

'''#解析資料

#find 獲取匹配的第乙個元素

soup = bs(html,'lxml')

print soup.prettify

div = soup('div',class_='inner-left f1')

print div.find('h1').text

Python網路爬蟲基礎

爬蟲,即網路爬蟲,大家可以理解為在網路上爬行的一直蜘蛛,網際網路就比作一張 大網,而爬蟲便是在這張網上爬來爬去的蜘蛛咯,如果它遇到資源,那麼它就會抓取下來。因此,使用者看到的網頁實質是由 html 構成的,爬蟲爬來的便是這 些內容,通過分析和過濾這些 html 實現對 文字等資源的獲取。url,即統...

python網路爬蟲一

正規表示式詳細介紹 import urllib2 import random 瀏覽器資訊 mozilla 5.0 windows u windows nt 6.1 en gb rv 1.9.1.17 gecko 20110123 like firefox 3.x seamonkey 2.0.12 m...

Python網路爬蟲 一 什麼是爬蟲

網路爬蟲 web crawler 一般被用來採集網路上的資訊,也叫做網路鏟 web scraper 網路蜘蛛 web spider 顧名思義,網路爬蟲就是先爬到要採集的網頁上,然後對資料資訊進行採集。1.了解爬蟲的工作原理 2.http抓取工具 3.高階功能 網際網路最早是作為科研資訊的一種交流方式...