码迷,mamicode.com
首页 > 其他好文 > 详细

第一次爬虫实例

时间:2018-09-02 21:55:34      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:current   with open   browser   web   style   dex   ade   并且   shutdown   

第一次爬虫实例

1、这是我第一次写的爬虫实例,写的不好请见谅,最后发现爬取的次数多了,被网站拉黑了,还是需要代理才行,代理还不太清楚怎么弄就先这样了

后面请大神可以帮忙改下怎么使用代理爬取。

第一次爬取网站的所有电影信息(仅供参考)

具体思路就是先获取第一页上的信息

然后根据翻页上的页数来确定爬取多少页

#下一步就是要写怎么爬取这些电影的种子并且下载到本地,等有时间了在写

下面是具体代码:

import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor


root = http://www.btrenren.com

def  bt_video(num):
    url = "http://www.btrenren.com/index.php/Index/index/p/%s.html"%num
    head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER"}

    html = requests.get(url=url,headers=head)
    soup = BeautifulSoup(html.content,lxml)
    data = soup.find(div,class_="ml")
    data = data.find_all(class_="title")
    with open("video.txt", a, encoding=utf-8) as f:
        for i in data:
            #电影名
            name = i.find_all(font)[1].get_text().split(/)[0]
            #日期国家
            vlue = i.find_all(class_=des)[0].get_text().split(\n)[0]
            #演员
            vlue1 = i.find_all(class_=des)[0].get_text().split(\n)[1]
            #豆瓣评分
            douban = i.find(class_="rt").get_text()
            #网页路径
            href = root + i.find_all(a)[1].get(href)
            #print(name,vlue,vlue1,douban,href)
            f.write(%s,%s,%s,%s,%s\n % (name, vlue, vlue1, douban,href))
    print(num)


def num_video():
    url = "http://www.btrenren.com/index.php/Index/index/p/1.html"
    head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER"}
    html = requests.get(url=url, headers=head)
    soup = BeautifulSoup(html.content, lxml)
    number = soup.find(ul,class_="pagelist")
    number = number.find_all(span,class_="rows")
    num = number[0].get_text().split( )[1]
    return num





if __name__ == "__main__":
    executor = ThreadPoolExecutor(max_workers=30)
    num = int(num_video())
    for i in range(1,num):
        executor.submit(bt_video,i)
    executor.shutdown()
    exit()

 

第一次爬虫实例

标签:current   with open   browser   web   style   dex   ade   并且   shutdown   

原文地址:https://www.cnblogs.com/xu743876685/p/9575254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!