找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 105|回复: 15

感谢一下eqblog大佬,我也学会写爬虫了,哈哈哈

[复制链接]

162

主题

354

回帖

1254

积分

金牌会员

积分
1254
发表于 2018-6-3 23:53:59 | 显示全部楼层 |阅读模式
本帖最后由 流量之神 于 2018-6-4 01:08 编辑





水平还是很菜的,哈哈,不过爬虫跑起来了挺有意思,找了一个简单的网站练手http://www.mdyuepai.com/

[ol]
  • import re
  • import requests
  • import json
  • from multiprocessing import Pool
  • from requests.exceptions import RequestException
  • import os
  • from hashlib import md5
  • from multiprocessing import Pool
  • def get_page_index(offset):
  •     headers = {
  •         'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
  •     }
  •     url = 'http://www.mdyuepai.com/?page=' + str(offset)
  •     try:
  •         response = requests.get(url, headers = headers)
  •         if response.status_code == 200:
  •             return response.text
  •         return None
  •     except RequestException:
  •         print('请求索引页出错')
  •         return None
  • def parse_index_page(html):
  •     pattern = re.compile('', re.S)
  •     items = re.findall(pattern, html)
  •     for item in items:
  •         yield item
  • def get_page_detail(url):
  •     try:
  •         response = requests.get(url)
  •         if response.status_code == 200:
  •             return response.text
  •         return None
  •     except ConnectionError:
  •         print('Error occurred')
  •         return None
  • def save_image(content):
  •     file_path = '{0}/{1}.{2}'.format('/home/', md5(content).hexdigest(), 'jpg')
  •     print(file_path)
  •     if not os.path.exists(file_path):
  •         with open(file_path, 'wb') as f:
  •             f.write(content)
  •             f.close()
  • def download_image(url):
  •     print('Downloading', url)
  •     try:
  •         response = requests.get(url)
  •         if response.status_code == 200:
  •             save_image(response.content)
  •         return None
  •     except ConnectionError:
  •         return None
  • def parse_page_detail(html):
  •     pattern = re.compile('class="item_infor_img.*?src="(.*?)".*?', re.S)
  •     images = re.findall(pattern, html)
  •     for image in images:
  •         download_image(image)
  • def main():
  •     for offset in range(20):
  •         html = get_page_index(offset)
  •         for item in parse_index_page(html):
  •             url = 'http://www.mdyuepai.com/'+ item
  •             html2 = get_page_detail(url)
  •             images = parse_page_detail(html2)
  • if __name__ == '__main__':
  •     pool = Pool()
  •     pool.map(main())[/ol]复制代码
  • 回复

    使用道具 举报

    19

    主题

    1178

    回帖

    2463

    积分

    金牌会员

    积分
    2463
    发表于 2018-6-3 23:54:49 | 显示全部楼层
    兔子大佬?
    回复

    使用道具 举报

    44

    主题

    298

    回帖

    764

    积分

    高级会员

    积分
    764
    发表于 2018-6-3 23:55:38 | 显示全部楼层
    膜拜

    回复

    使用道具 举报

    33

    主题

    914

    回帖

    2003

    积分

    金牌会员

    积分
    2003
    发表于 2018-6-3 23:57:25 | 显示全部楼层
    被爬的,那个站页面打开有点慢~
    回复

    使用道具 举报

    586

    主题

    2413

    回帖

    6698

    积分

    论坛元老

    积分
    6698
    发表于 2018-6-4 00:06:22 | 显示全部楼层

    Mishaelre 发表于 2018-6-3 23:54

    兔子大佬?

    什么兔子,他外号叫兔子吗
    回复

    使用道具 举报

    162

    主题

    354

    回帖

    1254

    积分

    金牌会员

    积分
    1254
     楼主| 发表于 2018-6-4 00:09:03 | 显示全部楼层
    "

    花名胖兔子,因为他那个头像呀~
    回复

    使用道具 举报

    36

    主题

    761

    回帖

    1696

    积分

    金牌会员

    积分
    1696
    发表于 2018-6-3 23:54:00 | 显示全部楼层
    在爬虫中多进程作用很小,要改成多线程
    回复

    使用道具 举报

    118

    主题

    1967

    回帖

    4428

    积分

    论坛元老

    积分
    4428
    发表于 2018-6-4 00:22:29 | 显示全部楼层
    8楼说的对

    回复

    使用道具 举报

    148

    主题

    871

    回帖

    2234

    积分

    金牌会员

    积分
    2234
    发表于 2018-6-4 00:09:00 | 显示全部楼层
    刚打开你的帖子 我发现有只虫子在我屏幕上爬

    你这个,不是线程池啊
    回复

    使用道具 举报

    439

    主题

    3332

    回帖

    8051

    积分

    论坛元老

    积分
    8051
    发表于 2018-6-5 01:12:00 | 显示全部楼层
    大佬 怎么学的? 还有约拍 让我想到了小鸟酱
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|Discuz! X

    GMT+8, 2025-5-8 18:57 , Processed in 0.022142 second(s), 3 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表