爬虫入门(1)

       操作系统:ubuntu
       IDE:pychar(http://www.jetbrains.com/pycharm/)
       语言:python

python确实是一门很有意思的语言,对于python,我的观点是:输出倒逼输入,一味的把一本编程语言从头看到尾,并没有什么用,接下来,直接上代码,不上代码的就是流氓
第一种:常规request:
我们用request请求并返回一个特定的post

from bs4 import BeautifulSoup
import requests
import time
url = 'https://www.tripadvisor.cn/'
requests.get(url)

这是对猫途鹰旅游网的访问请求头,接下来,我们要储存并开始进行网页源码的分析

wb_data = requests.get(url)#保存起来
soup = BeautifulSoup(wb_data.text,'lxml')#转换为text,用lxml模块进行分析
#访问酒店名称,图片,价钱
titles = soup.select('div.title > a')
imgs = soup.select('img[width="368"]')
hotals = soup.select('div.counts')

对接受后的名称价钱进行分析处理并且打印

 for title,img,hotal in zip(titles,imgs,hotals):
    data = {
        'title': title.get_text(),
        'img': img.get('src'),
        'hotal': list(hotal.stripped_strings),
    }
    print(data)

这时候我们想下载30页的东西,我们观看网址发现有个东西每一页加30于是从这个入手

url = ['https://www.tripadvisor.cn/Hotels-g294212-oa{}-Beijing-Hotels.html'.format(str(i)) for i in range(30, 900, 30)]

用这种方法,再用time模块,每2秒更新一次,以防被封

昆泰嘉华酒店
['¥698']
None
北京瑰丽酒店
['¥2,099']
None
王府半岛酒店
['¥2,332']
https://ccm.ddcdn.com/ext/photo-l/13/af/f9/e0/kuntai-royal-hotel.jpg
北京161酒店-雍和宫四合院店
['¥966', '带删除线的价格为合作伙伴对此报价回复的最高价格']
https://ccm.ddcdn.com/ext/photo-s/13/af/f9/e0/kuntai-royal-hotel.jpg
王府井希尔顿酒店
['¥671']
https://ccm.ddcdn.com/ext/photo-l/07/44/64/61/lobby.jpg

可是北京有那么多的酒店,我们就爬一页远远不够,对此我们来看一下那些网页地址,发现一些关键
#第一页网址
https://www.tripadvisor.cn/Hotels-g294212-oa30-Beijing-Hotels.html
#第二页网址
https://www.tripadvisor.cn/Hotels-g294212-oa60-Beijing-Hotels.html
#第三页网址
https://www.tripadvisor.cn/Hotels-g294212-oa90-Beijing-Hotels.html
发现了规律:以oa的数值加三十作为翻页的根据
对此设定url

   #每次给oa加30表示翻页
   url = ['https://www.tripadvisor.cn/Hotels-g294212-oa{}-Beijing-Hotels.html'.format(str(i)) for i in range(30, 900, 30)]

但是别忘了,对于同一个网页不间断的访问会封IP,所以我们导入time模块,并且设置访问间隔

import time
   time.sleep(2)

当然循环语句对于url这个元组的各个元素进行访问,设置函数解决问题的方法

  def a(a):
    time.sleep(2)#访问间隔睡眠时间
    wb_data = requests.get(a)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    titles = soup.select('div.listing_title > a')
    prices = soup.select('div.price-wrap > div')
    imgs = soup.select('div.inner')
    for title,price,img in zip(titles,prices,imgs):
        data = {
            'title': title.get_text(),
            'price': list(price.stripped_strings),
            'img': img.get('data-lazyurl')
        }

        print(data)
        with open('a.txt','a+') as f:
                for key in data:
                    f.write(str(data[key])+"\n")
        f.close()

    def main():
         for i in url:
				 a(i)

	if __name__ == '__main'
	    main()

这样我们就可以看到这个目录下的a.txt文件在加载内容
爬虫入门(1)
完整代码

import time
import requests
from bs4 import BeautifulSoup
#头文件
url = ['https://www.tripadvisor.cn/Hotels-g294212-oa{}-Beijing-Hotels.html'.format(str(i)) for i in range(30, 900, 30)]
#处理函数
def a(a):
    time.sleep(2)
    wb_data = requests.get(a)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    titles = soup.select('div.listing_title > a')
    prices = soup.select('div.price-wrap > div')
    imgs = soup.select('div.inner')
    for title,price,img in zip(titles,prices,imgs):
        data = {
            'title': title.get_text(),
            'price': list(price.stripped_strings),
            'img': img.get('data-lazyurl')
        }

        print(data)
        with open('a.txt','a') as f:
                for key in data:
                    f.write(str(data[key])+"\n")
        f.close()

   def main():
             for i in url:
   					 a(i)

   if __name__ == '__main'
   		    main()