Python爬蟲學習筆記

2021-08-27 15:59:06 字數 3719 閱讀 8909

1.使用build_opener()修改報頭:

headers = ("user-agent",           #定義變數headers儲存user-agent資訊           

opener = urllib.request.build_opener() #建立opener物件並賦給變數

openeropener.addheaders = [headers] #設定opener的addheaders

data = opener.open(url).read() #使用opener物件的open()方法開啟**

2.使用add_header()新增報頭

req = urllib.request.request(url)         #建立request物件

req.add_header("user-agent", #使用add_header()方法新增header資訊

data = urllib.request.urlopen(req).read() #使用urlopen開啟該request物件即可開啟對應**

格式:urllib.request.urlopen("", timeout=1)

keywd=」hello」

url=

+keywd

req=urllib.request.request(url)   #

建立request物件

data=urllib.request.urlopen(req).read()#

使用urlopen開啟該request物件即可開啟對應**

注意:當有中文是需進行重新編碼

key_wd=urllib.request.quote(keywd)

postdata = urllib.parse.urlencode().encode('utf-8')  #使用encode設定為utf-8編碼

req = urllib.request.request(url, postdata)req.add_header("user-agent",

def use_proxy(proxy_addr, url):     #使用**伺服器爬去取網頁(函式)    

import urllib.request

proxy = urllib.request.proxyhandler() #設定對應的**伺服器資訊

opener = urllib.request.build_opener(proxy, urllib.request.httphandler) #建立乙個自定義 opener物件

urllib.request.install_opener(opener) #建立全域性預設的opener物件,所以下面可以直接開啟**

data = urllib.request.urlopen(url).read() #.decode('utf-8') return dataproxy_

addr = "60.255.186.169:8888"

data = use_proxy(proxy_addr, "")

1.原子:正規表示式中最基本的組成單位

普通字元做原子

(「a」,」b」)

非列印字元做原子

(「\n」,」\t」)

通用字元做原子:

\w:匹配任意乙個字母,數字或下劃線

\w:匹配除上述之外的任意字元

\d:匹配任意乙個十進位制數

\d: 匹配除十進位制數之外的任意字元

\s:匹配任意乙個空白字元

\s:匹配除空白字元之外的任意字元

原子表:定義一組原子,匹配的時候表中的各原子逐一進行匹配

2.元字元:正規表示式具有一些特殊含義的字元

.匹配除換行符以外的任意字元

ˆ匹配字串開始的位置

$匹配字串結束的位置

*匹配0次或多次前面的原子

?匹配0次或1次前面的字元

+匹配1次或多次前面的字元

前面的字元恰好出現n次  (字元可以是通用字元)

前面的原子至少出現n次

前面的原子至少出現n次至多出現m次

|選擇模式符

()模式單元符 :在不改變正規表示式的情況下改變其含義,從而實現匹配結果的調整。

re.match()

從字串開始匹配乙個模式

格式:re.match(pattern,string,flag)    #flag為可選引數,可以放模式修正符等

re.search()

在全文進行檢索並匹配

全域性匹配函式

將符合模式的式子全部找出

格式:result = re.compile(pattern).findall(string)

re.sub()

根據正規表示式實現替換某些字串

格式:re.sub(pattern,rep,string,max)

rep:要替換成的字串

max:可選引數,代表最多替換的次數。如果不寫,則會將符合模式的結果全部替換

1.使用post模擬登入並儲存cookie

postdata = urllib.parse.urlencode().encode('utf-8')

req = urllib.request.request(url, postdata) # 構建物件

req.add_header("user-agent", # 新增表頭

cjar = http.cookiejar.cookiejar() # 建立cookiejar物件

opener = urllib.request.build_opener(urllib.request.httpcookieprocessor(cjar)) # 使用httpcookieprocessor建立cookie處理器,並以其為引數構建opener處理器

urllib.request.install_opener(opener) # 將opener設為全域性

file = opener.open(req)data = file.read() # 請求資訊並讀取

fhandle = open("c:\\users\\john\\desktop\\summer\\crawler\\htmlfile\\cookiejar_test.html", 'wb')

fhandle.write(data)fhandle.close()

2.使用已知cookie模擬登陸

request_header = {    

"accept": '*/*',

"accept-language": "en-us, en; q=0.8, zh-hans-cn; q=0.5, zh-hans; q=0.3",

'host': 'jw.jluzh.com',

"connection": "keep-alive",

"cookie": cookie_str,

req = urllib.request.request(url, headers=request_header)

resp = urllib.request.urlopen(req)

data = resp.read() #.decode('utf-8')

python爬蟲學習筆記

一 爬蟲思路 對於一般的文章而言,思路如下 1.通過主頁url獲取主頁原始碼,從主頁原始碼中獲得 標題 鏈結 如想要抓取知乎上的新聞,就獲得主頁上的新聞鏈結 2.繼續通過 標題 鏈結獲得 標題 原始碼,進而獲得 標題 中的內容。其中,當存在多頁時,先將每一頁都一樣的url寫下來,然後迴圈加入頁碼,具...

python爬蟲學習筆記

2.網頁資訊提取 beautiful soup庫 這是 學習北理的嵩山天老師mooc教程的筆記,是老師上課用的例項。import requests url try kv 將爬蟲偽裝成瀏覽器 r requests.get url,headers kv r.raise for status print ...

Python 爬蟲 學習筆記

爬取搜狗首頁的頁面資料 import requests if name main 指定url url 發起請求 get方法會返回乙個響應物件 response requests.get url url 獲取響應資料,text返回的是字串形式的響應資料 page text response.text ...