为了elasticsearch的搜索数据写了几个爬虫,下面分析一下思路和写法 。

    import requests
    import urllib3
    import urllib.request
    import sys, urllib
    from lxml import html
    from bs4 import BeautifulSoup
    import re
from datetime import datetime
from elasticsearch import Elasticsearch
import time
from scrapy.spider import Spider
import string
data = {"id":"","passwd":" "}
s = requests.Session()
result =s.post("http://m.byr.cn/user/login",data)
print(result)
def work(p):
q=str(p)
es = Elasticsearch()
url = "http://m.byr.cn/board/Food/1?p="+q #网页地址
wp=s.get(url)
cont=wp.content.decode("utf-8")
   ## print(cont)
soup=BeautifulSoup(cont,"lxml")
   ## print('\n')
#print(soup.prettify())
#print('\n')
   # print('\n')
url_list = soup.findAll(name='a',href=re.compile('article'))
url1=[]
i=0
list=[]
for each_url in url_list:
str_url = str(each_url).split('"')
list.append(str_url[1])
print(str_url[1])
list.remove(list[0])
print(list)
st1=''
for link in soup.findAll('a'):
st2=str(link.string)
st1=st1+st2
st3='●'
st4=st1[st1.find(st3)+1:]
st5=re.split('●|Re|├',st4)
length=min(len(list),len(st5))
print(st5)
if length>0:
for i in range(0,length):
es.index(index="byr", doc_type="info", id=datetime.now(),body={"title":st5[i], "timestamp": datetime.now(),"url":"http://m.byr.cn"+list[i]})
for v in range(1,37):
work(v)

由于byrbbs需要登录,这里采用手机版的登录页面,直接post账号密码过去就可以登录了。
接着定义函数work函数,接受一个参数p为Food版的页码,由于接收的为int型,需要用str函数转换成string型才能加到url后面,接着get该URL的网页,使用美丽汤处理一下。
通过对网页的分析,得出url和标题都存在标签中。
先用美丽汤得到url的一部分

url_list = soup.findAll(name='a',href=re.compile('article'))

依次加入到list中,又因为有非帖子的链接,观察后remove掉第一个值。
接着处理标题,同样处理a标签,为了除去噪音,我们先相加得到一个长的字符串。
然后通过观察,用特殊的●|Re|├分隔符对字符串进行分割,得到的就是各个标题了。
对应标题循环es写入数据,爬虫结束。

data = {"id":"“,"passwd":""}
s = requests.Session()
result =s.post("http://m.byr.cn/user/login",data)
print(result)
es = Elasticsearch()
def work(ti,p):
q=str(p)
id1=ti+q
tit="/article/"+ti+"?au="
url = "http://m.byr.cn/article/"+ti+"/"+q
wp=s.get(url)
cont=wp.content.decode("utf-8")
print(cont)
title=re.search('<li class="f">(.*?)</li>', cont)
page=re.search('<div class="sp">(.*?)</div>', cont)
if(title is None):
    return
estitle=str(title.group(1))
espage=str(page.group(0))

print(estitle)
print(espage)
#es.indices.create(index='byrbbs',ignore=400)
es.index(index="byrbbs", doc_type="info", id=id1,body={"title":estitle, "timestamp": datetime.now(),"url":url,"page":espage})

#soup=BeautifulSoup(cont,"lxml")
#print('\n')
#print(soup.prettify())
for i in range(18300,19300):
work("BBShelp",i)

接着第二个爬虫,还是byrbbs的,思路有所不同。
是按照版块&帖子的ID进行爬取。

import requests
import re
from elasticsearch import Elasticsearch
from datetime import datetime
def searchtitle(t1):
    txt=t1
    re1='.*?'  # Non-greedy match on filler
    re2='".*?"'    # Uninteresting: string
    re3='.*?'  # Non-greedy match on filler
    re4='".*?"'    # Uninteresting: string
    re5='.*?'  # Non-greedy match on filler
    re6='".*?"'    # Uninteresting: string
    re7='.*?'  # Non-greedy match on filler
    re8='".*?"'    # Uninteresting: string
    re9='.*?'  # Non-greedy match on filler
    re10='(".*?")' # Double Quote String 1
    rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10,re.IGNORECASE|re.DOTALL)
    m = rg.search(txt)
    if m:
        string1=m.group(1)
        print ("("+string1+")"+"\n")
        return ("("+string1+")"+"\n")
def searchurl(t2):
    re1='.*?'  # Non-greedy match on filler
    re2='".*?"'    # Uninteresting: string
    re3='.*?'  # Non-greedy match on filler
    re4='".*?"'    # Uninteresting: string
    re5='.*?'  # Non-greedy match on filler
    re6='".*?"'    # Uninteresting: string
    re7='.*?'  # Non-greedy match on filler
    re8='".*?"'    # Uninteresting: string
    re9='.*?'  # Non-greedy match on filler
    re10='".*?"'   # Uninteresting: string
    re11='.*?' # Non-greedy match on filler
    re12='(".*?")' # Double Quote String 1

    rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,re.IGNORECASE|re.DOTALL)
    m = rg.search(t2)
    if m:
        string1=m.group(1)
        print ("("+string1+")"+"\n")
        return  "("+string1+")"+"\n"
def se(p):
    s=requests.session()
    q=str(p)
    url="http://list.iqiyi.com/www/1/1-------------11-"+q+"-1-iqiyi--.html"
    wp=s.get(url)
    cont=wp.content.decode("utf-8")
    print(cont)
    page=re.findall('<img (.*?)>', cont,re.S)
    for ii in range(1,len(page)):
        searchurl(page[ii])
        searchtitle(page[ii])
        es.index(index="film", doc_type="info", id=datetime.now(),body={"title":searchtitle(page[ii]), "timestamp": datetime.now(),"url":searchurl(page[ii])})
es=Elasticsearch()
es.index(index="film",doc_type="info",body={})
for i in range(1,30):
    se(i)

第三个爬虫是爱奇艺的
用了http://www.txt2re.com/来写正则表达式的筛选,非常方便,效率感天动地。

链接:https://github.com/qyc0129/byr-bbs-scrapy

下次可能会使用scrapy框架,虽然不太喜欢。。