鱼C论坛

 找回密码
 立即注册
查看: 752|回复: 3

关于Python爬取网页的问题

[复制链接]
发表于 2018-7-28 21:14:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我之前做了一个爬取煎蛋网的爬虫(小甲鱼教程那个),但是一直不能爬取成功,我一行一行的代码测试,终于找到原因,我爬取下来的源代码与审核元素(或者是检查)的代码不一样,简单来说,我用的QQ浏览器,右键单击有个“检查”选项,我在里面找到了我想要的图片地址,而右键单击有个“检查源代码”选项,然后我发现我在源代码里面找不到那个图片的地址了(我直接爬取首页网址,爬取下来后不仅用find查找了,我还一行一行的查找,结果还是找不到),这两种代码不一样。。。。。这种情况下我要怎么爬取图片呢,源代码里根本就没有
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-28 21:17:01 | 显示全部楼层
实际上我用小甲鱼的代码也爬取失败(当然,我把代理的一部分代码给删了,没有代理)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-28 21:30:43 From FishC Mobile | 显示全部楼层
考虑js加载。先抓包看图片地址生成规律。实在不行就解析js吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-28 21:33:15 | 显示全部楼层
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.support.wait import WebDriverWait
  5. from selenium.common.exceptions import TimeoutException
  6. import time
  7. import requests
  8. import os

  9. '''屏蔽掉浏览器界面'''
  10. URL = 'http://jandan.net/ooxx'
  11. chrome_options = webdriver.ChromeOptions()
  12. chrome_options.add_argument('--headless')
  13. browser = webdriver.Chrome(chrome_options = chrome_options)
  14. wait = WebDriverWait(browser, 10)
  15. browser.get(URL)


  16. def next_page():  # 点击下一页
  17.     button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'previous-comment-page')))
  18.     return button


  19. def cur_page():  # 获取当前页数
  20.     page = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'current-comment-page')))
  21.     return page.text


  22. def parse_html(lyst):
  23.     """
  24.     :param img:单个图片链接
  25.     :param lyst:存储图片链接
  26.     """
  27.     imgs_info = browser.find_elements_by_xpath('//*[@id="comments"]/ol/li//p/img')
  28.     for img in imgs_info:
  29.         img = img.get_attribute('src')
  30.         if img[len(img) - 3:] == 'jpg':  # 剔除广告的'.gif'图片
  31.             lyst.append(img)


  32. def dowmloader(url):
  33.     '''图片下载'''
  34.     try:
  35.         response = requests.get(url)
  36.         if response.status_code == 200:
  37.             return response.content
  38.         return None
  39.     except Exception:
  40.         pass


  41. def save_img(img_content, num):
  42.     '''
  43.     :param img_content:二进制数据
  44.     :param num:图片保存的次序
  45.     '''
  46.     with open(str(num) + '.jpg', 'wb') as f:
  47.         f.write(img_content)


  48. def jandan_crawlers(lyst):
  49.     '''
  50.     :param FLAG:抓取网页页数
  51.     '''
  52.     try:
  53.         current_page = cur_page()
  54.         FLAG = int(current_page[1:len(current_page) - 1])
  55.         while FLAG:
  56.             print('正在抓取煎蛋网第%d页图片' % FLAG)
  57.             parse_html(lyst)
  58.             time.sleep(3)
  59.             button = next_page()
  60.             button.click()
  61.             FLAG -= 1
  62.             if FLAG == 1:
  63.                 jandan_crawlers(lyst)
  64.                 FLAG = False
  65.         return lyst
  66.     except TimeoutException:
  67.         pass


  68. if __name__ == '__main__':
  69.     input = input('请输入保存路径:')  
  70.     os.mkdir(input)
  71.     os.chdir(input)  # 切换图片的保存路径
  72.     lyst = []
  73.     num = 1
  74.     url_info = jandan_crawlers(lyst)
  75.     for url in url_info:
  76.         img_content = dowmloader(url)
  77.         #print('正在保存图片:' + str(num) + '.jpg')
  78.         save_img(img_content, num)
  79.         num += 1
  80.     print('煎蛋网图片抓取完成')
  81.     browser.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-1 03:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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