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.