A. 如何用python抓取動態頁面信息
很早之前,學習Python web編程的時候,就涉及一個Python的urllib。可以用urllib.urlopen("url").read()可以輕松讀取頁面上面的靜態信息。但是,隨著時代的發展,也來越多的網頁中更多的使用javascript、jQuery、PHP等語言動態生成頁面信息。因此,用urllib再去抓取頁面HTML就不足以達到我們想要的效果。
回到頂部
解決思路:
有一個思路最為簡單的思路可以動態解析頁面信息。urllib不可以解析動態信息,但是瀏覽器可以。在瀏覽器上展現處理的信息其實是處理好的HTML文檔。這為我們抓取動態頁面信息提供了很好的思路。在Python中有一個很有名的圖形庫——PyQt。PyQt雖然是圖形庫,但是他裡面 QtWebkit。這個很實用。谷歌的Chrome和蘋果的Safari都是基於WebKit內核開發的,所以我們可以通過PyQt中得QtWebKit 把頁面中的信息讀取載入到HTML文檔中,再解析HTML文檔,從HTML文檔中提取我們想用得信息。
回到頂部
所需材料:
作者本人實用Mac OS X。應該在Windows和Linux平台也可以採用相同的辦法。
1、Qt4 library
Library,而不是Creator。Library在Mac的默認安裝路徑下,應該是/home/username/Developor/,不要改變Qt4的默認安裝路徑。否則可能安裝失敗。
官方網址:http://qt-project.org/downloads
2、SIP、PyQt4
這兩個軟體可以在在PyQt的官網找到。下載的是它的源碼。Mac和Linux需要自己編譯。
下載地址是:http://www.riverbankcomputing.co.uk/software/pyqt/download
在終端切換到文件解壓後的目錄中。
在終端中輸入
python configure.py
make
sudo make install
進行安裝編譯。
SIP和PyQt4兩個安裝方法相同。但是PyQt4依賴SIP。所以先安裝SIP再安裝PyQt4
1、2兩步完成之後,Python的PyQt4的模塊就安裝好了。在Python shell中輸入import PyQt4看看能不能找到PyQt4的模塊。
3、Spynner
spynner是一個QtWebKit的客戶端,它可以模擬瀏覽器,完成載入頁面、引發事件、填寫表單等操作。
這個模塊可以在Python的官網找到。
下載地址: https://pypi.python.org/pypi/spynner/2.5
解壓後,cd到安裝目錄,然後輸入sudo python configure.py install安裝該模塊。
這樣Spynner模塊就安裝完成了,在python shell中試試import spynner看看該模塊有沒有安裝完成。
回到頂部
Spynner的簡單使用
Spynner的功能十分強大,但是由於本人能力有限,就介紹一下如何顯示網頁的源碼吧。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/python
#-*-coding: utf-8 -*-
import spynner
browser = spynner.Browser()
#創建一個瀏覽器對象
browser.hide()
#打開瀏覽器,並隱藏。
browser.load("http://www..com")
#browser 類中有一個類方法load,可以用webkit載入你想載入的頁面信息。
#load(是你想要載入的網址的字元串形式)
print browser.html.encode("utf-8")
#browser 類中有一個成員是html,是頁面進過處理後的源碼的字元串.
#將其轉碼為UTF-8編碼
open("Test.html", 'w+').write(browser.html.encode("utf-8"))
#你也可以將它寫到文件中,用瀏覽器打開。
browser.close()
#關閉該瀏覽器
通過這個程序,就可以比較容易的顯示webkit處理的頁面HTML源碼了。
B. 使用python3進行網頁抓取
先用get方法獲取網頁源碼,然後用正則表達式提取需要的部分(能用一些庫更好)
C. python抓取網頁內容時出現如下錯誤應該怎麼辦
在Python自帶的互動式模式下編輯,互動式下,一行只能放一段代碼import requests , 這一行要和下面你定義的函數隔開為兩段代碼
也就是import requests 要按回車鍵,然後在新的【>>>】開始處再輸入你定義的函數代碼
D. Python 爬取https的登錄界面,怎麼爬取成功,謝謝
之前寫的一直沒成功,原因是用的不是HTTPS相關的函數。這次仔細研究了一下,有幾個需要注意的點,一個是POST模擬登陸的時候,header中的cookie值,不同的網站應該會有不同的要求;另一個是GET頁面的時候,是需要加上POST得到的response中的set-cookie的。這樣才能利用登陸的成功。
寫完POST和GET頁面後,順便寫了個簡單的命令行實現。
importhttplib,urllib
importurllib2
importcookielib
importsys
file_text="build_change.txt"
resultTable=dict()
host='buuuuuuu.knight.com'
defLogin(username,password,csrf=''):
url='/login/'
values={
'username':username,
'password':password,
'next':'',
'csrfmiddlewaretoken':csrf,
}
headers={
'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/35.0.1916.114Safari/537.36',
'Content-Type':'application/x-www-form-urlencoded',
'Connection':'keep-alive',
'Cookie':'csrftoken=%s'%csrf,
'Referer':'https://buuuuuuu.knight.com/login/',
'Origin':'https://buuuuuuu.knight.com',
'Content-Type':'application/x-www-form-urlencoded',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
}
values=urllib.urlencode(values)
conn=httplib.HTTPSConnection(host,443)
conn.request("POST",url,values,headers)
response=conn.getresponse()
print'Login:',response.status,response.reason
'''
hdata=response.getheaders()
foriinxrange(len(hdata)):
forjinxrange(len(hdata[i])):
printhdata[i][j],
'''
returnresponse.getheader("set-cookie")
defGetHtml(_url,cookie):
get_headers={
'Host':'xxxxx.knight.com',
'Connection':'keep-alive',
'Cache-Control':'max-age=0',
'Cookie':cookie,
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/35.0.1916.114Safari/537.36',
'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
}
conn=httplib.HTTPSConnection(host)
conn.request("GET",_url,None,get_headers)
res2=conn.getresponse()
print"Get%s:"%_url,res2.status,res2.reason
'''
hdata1=res2.getheaders()
foriinxrange(len(hdata1)):
forjinxrange(len(hdata1[i])):
printhdata1[i][j],
'''
data=res2.read()
fp=open("build_change.txt","w")
fp.write(data)
fp.close()
defParseHtml():
fp=open(file_text,"r")
content=fp.readline()
_pos=0
whilecontent:
ifcontent.find("class="change-body"")>=0:
topic=content.split(">")
resultTable[_pos]=topic[1]
whilecontent:
content=fp.readline()
resultTable[_pos]=resultTable[_pos]+content
ifcontent.find("</div>")>=0:
_pos=_pos+1
break
content=fp.readline()
fp.close()
print"Parsehtmlsuccess."
defGenerateResultTxt():
f=open("build_change_result.txt","w")
forminresultTable.keys():
f.write("------------------------------------------------------------------------------------------- ")
f.write(resultTable[m])
f.close()
print"Generateresultsuccess:build_change_result.txt."
defHelp():
print'-h:help'
print'-u:username(must)'
print'-p:password(must)'
print'-c:csrftoken(optional)'
print'-s:sandboxbuildid(must)'
print'Forexample:'
print'[1]pythonBuildChange.py-h'
print'[2]pythonBuildChang.py-uu-pp-ss1s2'
print'[3]pythonBuildChang.py-uu-pp-cc-ss1s2'
defParseParam(com):
length=len(com)
username=""
password=""
csrf=""
sid1=""
sid2=""
iflength==2orlength==8orlength==10:
ifcom[1]=='-h':
Help()
foriinrange(1,length):
ifcom[i]=='-u'andi<(length-1):
username=com[i+1]
i+=1
elifcom[i]=='-p'andi<(length-1):
password=com[i+1]
i+=1
elifcom[i]=='-c'andi<(length-1):
csrf=com[i+1]
i+=1
elifcom[i]=='-s'andi<(length-2):
sid1=com[i+1]
sid2=com[i+2]
i+=2
ifusername==""orpassword==""orsid1==""orsid2=="":
print'[Error]Parametererror!'
print'[Error]Youcanuse"pythonBuildChange.py-h"toseehowcanusethisscript.'
else:
ifcsrf=="":
cookie=Login(username,password)
else:
cookie=Login(username,password,csrf)
_url="//changelog//between//%s//and//%s/"%(sid1,sid2)
GetHtml(_url,cookie)
ParseHtml()
GenerateResultTxt()
#C:Python27python.exeC:UsersknightDesktopuildBuildChange.py-uxux-pKKKKKKKK-s18594091858525
if__name__=="__main__":
ParseParam(sys.argv)
E. 如何用 Python 爬取需要登錄的網站
步驟一:研究該網站
打開登錄頁面
進入以下頁面 「bitbucket.org/account/signin」。你會看到如下圖所示的頁面(執行注銷,以防你已經登錄)
仔細研究那些我們需要提取的詳細信息,以供登錄之用
在這一部分,我們會創建一個字典來保存執行登錄的詳細信息:
1. 右擊 「Username or email」 欄位,選擇「查看元素」。我們將使用 「name」 屬性為 「username」 的輸入框的值。「username」將會是 key 值,我們的用戶名/電子郵箱就是對應的 value 值(在其他的網站上這些 key 值可能是 「email」,「 user_name」,「 login」,等等)。
2. 右擊 「Password」 欄位,選擇「查看元素」。在腳本中我們需要使用 「name」 屬性為
「password」 的輸入框的值。「password」 將是字典的 key 值,我們輸入的密碼將是對應的 value
值(在其他網站key值可能是 「userpassword」,「loginpassword」,「pwd」,等等)。
3. 在源代碼頁面中,查找一個名為 「csrfmiddlewaretoken」 的隱藏輸入標簽。「csrfmiddlewaretoken」
將是 key 值,而對應的 value 值將是這個隱藏的輸入值(在其他網站上這個 value 值可能是一個名為 「csrftoken」,「 authenticationtoken」 的隱藏輸入值)。列如:「」。
最後我們將會得到一個類似這樣的字典:
Python
payload = {
"username": "<USER NAME>",
"password": "<PASSWORD>",
"csrfmiddlewaretoken": "<CSRF_TOKEN>"
}
1
2
3
4
5
payload = {
"username": "<USER NAME>",
"password": "<PASSWORD>",
"csrfmiddlewaretoken": "<CSRF_TOKEN>"
}
請記住,這是這個網站的一個具體案例。雖然這個登錄表單很簡單,但其他網站可能需要我們檢查瀏覽器的請求日誌,並找到登錄步驟中應該使用的相關的 key 值和 value 值。
步驟2:執行登錄網站
對於這個腳本,我們只需要導入如下內容:
Python
import requests
from lxml import html
1
2
import requests
from lxml import html
首先,我們要創建 session 對象。這個對象會允許我們保存所有的登錄會話請求。
Python
session_requests = requests.session()
1
session_requests = requests.session()
第二,我們要從該網頁上提取在登錄時所使用的 csrf 標記。在這個例子中,我們使用的是 lxml 和 xpath 來提取,我們也可以使用正則表達式或者其他的一些方法來提取這些數據。
Python
login_url = "https://bitbucket.org/account/signin/?next=/"
result = session_requests.get(login_url)
tree = html.fromstring(result.text)
authenticity_token = list(set(tree.xpath("//input[@name='csrfmiddlewaretoken']/@value")))[0]
1
2
3
4
5
login_url = "https://bitbucket.org/account/signin/?next=/"
result = session_requests.get(login_url)
tree = html.fromstring(result.text)
authenticity_token = list(set(tree.xpath("//input[@name='csrfmiddlewaretoken']/@value")))[0]
**更多關於xpath 和lxml的信息可以在這里找到。
接下來,我們要執行登錄階段。在這一階段,我們發送一個 POST 請求給登錄的 url。我們使用前面步驟中創建的 payload 作為 data 。也可以為該請求使用一個標題並在該標題中給這個相同的 url 添加一個參照鍵。
Python
result = session_requests.post(
login_url,
data = payload,
headers = dict(referer=login_url)
)
1
2
3
4
5
result = session_requests.post(
login_url,
data = payload,
headers = dict(referer=login_url)
)
步驟三:爬取內容
現在,我們已經登錄成功了,我們將從 bitbucket dashboard 頁面上執行真正的爬取操作。
Python
url = 'https://bitbucket.org/dashboard/overview'
result = session_requests.get(
url,
headers = dict(referer = url)
)
1
2
3
4
5
url = 'https://bitbucket.org/dashboard/overview'
result = session_requests.get(
url,
headers = dict(referer = url)
)
為了測試以上內容,我們從 bitbucket dashboard 頁面上爬取了項目列表。我們將再次使用
xpath 來查找目標元素,清除新行中的文本和空格並列印出結果。如果一切都運行 OK,輸出結果應該是你 bitbucket 賬戶中的
buckets / project 列表。
Python
tree = html.fromstring(result.content)
bucket_elems = tree.findall(".//span[@class='repo-name']/")
bucket_names = [bucket.text_content.replace("n", "").strip() for bucket in bucket_elems]
print bucket_names
1
2
3
4
5
tree = html.fromstring(result.content)
bucket_elems = tree.findall(".//span[@class='repo-name']/")
bucket_names = [bucket.text_content.replace("n", "").strip() for bucket in bucket_elems]
print bucket_names
你也可以通過檢查從每個請求返回的狀態代碼來驗證這些請求結果。它不會總是能讓你知道登錄階段是否是成功的,但是可以用來作為一個驗證指標。
例如:
Python
result.ok # 會告訴我們最後一次請求是否成功
result.status_code # 會返回給我們最後一次請求的狀態
1
2
result.ok # 會告訴我們最後一次請求是否成功
result.status_code # 會返回給我們最後一次請求的狀態
就是這樣。
F. Python怎麼抓https的包
burp有個插件叫gason,可以直接把請求的數據包發送給sqlmap來測試注入,get/post均可
也有國內的安全研究者開發了burp的插件可以很方便的和sqlmap結合起來
G. 如何用Python爬蟲抓取網頁內容
首先,你要安裝requests和BeautifulSoup4,然後執行如下代碼.
importrequests
frombs4importBeautifulSoup
iurl='http://news.sina.com.cn/c/nd/2017-08-03/doc-ifyitapp0128744.shtml'
res=requests.get(iurl)
res.encoding='utf-8'
#print(len(res.text))
soup=BeautifulSoup(res.text,'html.parser')
#標題
H1=soup.select('#artibodyTitle')[0].text
#來源
time_source=soup.select('.time-source')[0].text
#來源
origin=soup.select('#artibodyp')[0].text.strip()
#原標題
oriTitle=soup.select('#artibodyp')[1].text.strip()
#內容
raw_content=soup.select('#artibodyp')[2:19]
content=[]
forparagraphinraw_content:
content.append(paragraph.text.strip())
'@'.join(content)
#責任編輯
ae=soup.select('.article-editor')[0].text
這樣就可以了
H. 怎樣用python爬取網頁
#coding=utf-8
importurllib
importre
#網路貼吧網址:https://tieba..com/index.html
#根據URL獲取網頁HTML內容
defgetHtmlContent(url):
page=urllib.urlopen(url)
returnpage.read()
#從HTML中解析出所有jpg的圖片的URL
#從HTML中jpg格式為<img...src="xxx.jpg"width='''>
defgetJPGs(html):
#解析jpg圖片URL的正則表達式
jpgReg=re.compile(r'<img.+?src="(.+?.jpg)"')
#解析出jpg的URL列表
jpgs=re.findall(jpgReg,html)
returnjpgs
#用圖片url下載圖片並保存成制定文件名
defdownloadJPG(imgUrl,fileName):
urllib.urlretrieve(imgUrl,fileName)
#批量下載圖片,默認保存到當前目錄下
defbatchDownloadJPGs(imgUrls,path='../'):#path='./'
#給圖片重命名
count=1
forurlinimgUrls:
downloadJPG(url,''.join([path,'{0}.jpg'.format(count)]))
print"下載圖片第:",count,"張"
count+=1
#封裝:從網路貼吧網頁下載圖片
defdownload(url):
html=getHtmlContent(url)
jpgs=getJPGs(html)
batchDownloadJPGs(jpgs)
defmain():
url="http://www.meituba.com/dongman/"
download(url)
if__name__=='__main__':
main()
I. 求python抓網頁的代碼
python3.x中使用urllib.request模塊來抓取網頁代碼,通過urllib.request.urlopen函數取網頁內容,獲取的為數據流,通過read()函數把數字讀取出來,再把讀取的二進制數據通過decode函數解碼(編號可以通過查看網頁源代碼中<meta http-equiv="content-type" content="text/html;charset=gbk" />得知,如下例中為gbk編碼。),這樣就得到了網頁的源代碼。
如下例所示,抓取本頁代碼:
importurllib.request
html=urllib.request.urlopen('
).read().decode('gbk')#注意抓取後要按網頁編碼進行解碼
print(html)
以下為urllib.request.urlopen函數說明:
urllib.request.urlopen(url,
data=None, [timeout, ]*, cafile=None, capath=None,
cadefault=False, context=None)
Open the URL url, which can be either a string or a Request object.
data must be a bytes object specifying additional data to be sent to
the server, or None
if no such data is needed. data may also be an iterable object and in
that case Content-Length value must be specified in the headers. Currently HTTP
requests are the only ones that use data; the HTTP request will be a
POST instead of a GET when the data parameter is provided.
data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or
sequence of 2-tuples and returns a string in this format. It should be encoded
to bytes before being used as the data parameter. The charset parameter
in Content-Type
header may be used to specify the encoding. If charset parameter is not sent
with the Content-Type header, the server following the HTTP 1.1 recommendation
may assume that the data is encoded in ISO-8859-1 encoding. It is advisable to
use charset parameter with encoding used in Content-Type header with the Request.
urllib.request mole uses HTTP/1.1 and includes Connection:close header
in its HTTP requests.
The optional timeout parameter specifies a timeout in seconds for
blocking operations like the connection attempt (if not specified, the global
default timeout setting will be used). This actually only works for HTTP, HTTPS
and FTP connections.
If context is specified, it must be a ssl.SSLContext instance describing the various SSL
options. See HTTPSConnection for more details.
The optional cafile and capath parameters specify a set of
trusted CA certificates for HTTPS requests. cafile should point to a
single file containing a bundle of CA certificates, whereas capath
should point to a directory of hashed certificate files. More information can be
found in ssl.SSLContext.load_verify_locations().
The cadefault parameter is ignored.
For http and https urls, this function returns a http.client.HTTPResponse object which has the
following HTTPResponse
Objects methods.
For ftp, file, and data urls and requests explicitly handled by legacy URLopener and FancyURLopener classes, this function returns a
urllib.response.addinfourl object which can work as context manager and has methods such as
geturl() — return the URL of the resource retrieved,
commonly used to determine if a redirect was followed
info() — return the meta-information of the page, such
as headers, in the form of an email.message_from_string() instance (see Quick
Reference to HTTP Headers)
getcode() – return the HTTP status code of the response.
Raises URLError on errors.
Note that None
may be returned if no handler handles the request (though the default installed
global OpenerDirector uses UnknownHandler to ensure this never happens).
In addition, if proxy settings are detected (for example, when a *_proxy environment
variable like http_proxy is set), ProxyHandler is default installed and makes sure the
requests are handled through the proxy.
The legacy urllib.urlopen function from Python 2.6 and earlier has
been discontinued; urllib.request.urlopen() corresponds to the old
urllib2.urlopen.
Proxy handling, which was done by passing a dictionary parameter to urllib.urlopen, can be
obtained by using ProxyHandler objects.
Changed in version 3.2: cafile
and capath were added.
Changed in version 3.2: HTTPS virtual
hosts are now supported if possible (that is, if ssl.HAS_SNI is true).
New in version 3.2: data can be
an iterable object.
Changed in version 3.3: cadefault
was added.
Changed in version 3.4.3: context
was added.