效果的实现,你们可以直接复制后直接运行!我只用了xpath的这个方法,因为它最实在!橙色字体(如果我没有色盲的话),就是网页中定位的元素,可以找到的!
接下来就是与BeautifulSoup的结合了,但是我们看到的只是打开了网页,并没有源码,那么就需要 “变量名.page_source”这个方法,他会实现你的梦想,你懂得?
ht = driver.page_source #print ht 你可以Print出啦看看 soup = BeautifulSoup(ht,'html.parser')
下面就是BeautifulSoup的一些语法操作了,对于数据的结构还有采集,在上一篇里面有详细的抓取操作!!!
算了!说一个最简单的定位抓取:
soup = BeautifulSoup(ht,'html.parser')
a = soup.find('table',id="ctl00_ContentMain_SearchResultsGrid_grid")
if a: #必须加判断,不然访问的网页或许没有这一元素,程序就会都停止!class的标签必须是class_,一定要记住!
哈哈哈!mongodb了昂,细节细节,首先需要用到模块----from pymongo import MongoClient,ASCENDING, DESCENDING
因为在python,mongodb的语法仍然实用,所以需要定义一个库,并且是全局性的,还有链接你计算机的一个全局变量。
if __name__ == '__main__':
global db#全局变量
global table#全局数据库
table = 'mouser_product'
mconn=MongoClient("mongodb://localhost")#地址
db=mconn.test
db.authenticate('test','test')#用户名和密码
Taobao()定义这些后,需要我们的新id来对数据的跟踪加定义:
db.sn.find_and_modify({"_id": table}, update={ "$inc": {'currentIdValue': 1}},upsert=True)
dic = db.ids.find({"_id":table}).limit(1)
return dic[0].get("currentIdValue") 这个方法是通用的,所以只要记住其中的mongodb的语法就可以了!因为这里是有返回值的,所以这个是个方法体python网页自动化脚本,这里不需要太过于纠结是怎么实现的,理解就好,中心还是在存数据的过程中
count = db[table].find({'数据':数据}).count() #是检索数据库中的数据
if count <= 0: #判断是否有
ids= getNewsn() #ids就是我们新定义的id,这里的id是1开始的增长型id
db[table].insert({"ids":ids,"数据":数据}) 这样我们的数据就直接存入到mongodb的数据库中了,这里解释一下为什么在大数据中这么喜欢mongodb,因为它小巧,速度佳!
最后来一个实例源码:
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
from pymongo import MongoClient,ASCENDING, DESCENDING
import time
import re
def parser():
try:
f = open('sitemap.txt','r')
for i in f.readlines():
sorturl=i.strip()
driver = webdriver.Firefox()
driver.get(sorturl)
time.sleep(50)
ht = driver.page_source
#pageurl(ht)
soup = BeautifulSoup(ht,'html.parser')
a = soup.find('a',class_="first-last")
if a:
pagenum = int(a.get_text().strip())
print pagenum
for i in xrange(1,pagenum):
element = driver.find_element_by_xpath('//a[@id="ctl00_ContentMain_PagerTop_%s"]' %i)
element.click()
html = element.page_source
pageurl(html)
time.sleep(50)
driver.quit()
except Exception,e:
print e
def pageurl(ht):
try:
soup = BeautifulSoup(ht,'html.parser')
a = soup.find('table',id="ctl00_ContentMain_SearchResultsGrid_grid")
if a:
tr = a.find_all('tr',class_="SearchResultsRowOdd")
if tr:
for i in tr:
td = i.find_all('td')
if td:
url = td[2].find('a')
if url:
producturl = '网址'+url['href']
print producturl
count = db[table].find({"url":producturl}).count()
if count
for i in tr1:
td = i.find_all('td')
if td:
url = td[2].find('a')
if url:
producturl = '网址'+url['href']
print producturl
count = db[table].find({"url":producturl}).count()
if count<=0:
sn = getNewsn()
db[table].insert({"sn":sn,"url":producturl})
print str(sn) + ' inserted successfully'
time.sleep(3)
else:
print 'exists url'
#time.sleep(5)
except Exception,e:
print e
def getNewsn():
db.sn.find_and_modify({"_id": table}, update={ "$inc"{'currentIdValue': 1}},upsert=True)
dic = db.sn.find({"_id":table}).limit(1)
return dic[0].get("currentIdValue")
if __name__ == '__main__':
global db
global table
table = 'mous_product'
mconn=MongoClient("mongodb://localhost")
db=mconn.test
db.authenticate('test','test')
parser()这一串代码是破解一个老外的无聊验证码界面结缘的,我真的对他很无语了!破解方法还是实践中!这是完整的源码,无删改的哦!纯手工!
来源:【九爱网址导航www.fuzhukm.com】 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!