鱼C论坛

 找回密码
 立即注册
查看: 2014|回复: 2

[作品展示] 新人写的,使用数据库选择代理爬取图片(有爬取代理存入数据库的方法)

[复制链接]
发表于 2018-1-10 14:07:22 | 显示全部楼层 |阅读模式

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

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

x
  1. import pymysql
  2. import random
  3. from bs4 import BeautifulSoup
  4. import urllib.request
  5. import re
  6. import os


  7. def get_proxy():
  8.     #这里是连接数据库,获得所有代理,并存入列表返回
  9.     proxys_list=[]
  10.     conn=pymysql.connect(host="127.0.0.1",user="root",passwd="123456",db="mysql")

  11.     cur=conn.cursor()
  12.     cur.execute("USE scraping")
  13.     cur.execute("SELECT * FROM PROXYS_LIST WHERE ID")
  14.     for proxy in cur.fetchall():
  15.         proxys_list.append(proxy[1])
  16.     cur.close()
  17.     conn.close()
  18.     return proxys_list


  19. def choice_proxy():
  20.     #这里是通过random随机选择一个代理,并安装opener
  21.     headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
  22.     proxy_temp=random.choice(get_proxy())
  23.     proxy_support=urllib.request.ProxyHandler(eval(proxy_temp))
  24.     opener=urllib.request.build_opener(proxy_support)
  25.     opener.addheaders = [headers]
  26.     urllib.request.install_opener(opener)
  27.     print(proxy_temp)


  28. def get_content(url):
  29.     #这里首先选择代理,然后进行访问判断,如果失败,重新选择代理IP,重复上一步
  30.     choice_proxy()
  31.     while True:
  32.         try:
  33.             response=urllib.request.urlopen(url)
  34.         except Exception as e:
  35.             print("失败")
  36.             choice_proxy()
  37.             continue
  38.         else:
  39.             print("成功")
  40.         print("读取完成")
  41.         html=response.read()
  42.         return html

  43. def get_out_url():
  44.     #这里是爬取shaofu页面的所有内链接,然后把标题和链接分别存入两个列表中
  45.     global href_list,title_list
  46.     href_list=[]
  47.     title_list=[]
  48.     out_url="http://www.symmz.com/shaofu.html "
  49.     html=BeautifulSoup(get_content(out_url),"html.parser")
  50.     title=html.findAll("div",{"class":"title"})
  51.     for each in title:
  52.         in_to=each.findAll("a",{"href":re.compile(r"/shaofu/.+")},{"title":re.compile(r".+")})
  53.         for i in in_to:
  54.             href_list.append(i["href"])
  55.             title_list.append(i["title"])

  56. def download(in_url,title):
  57.     #用正则表达式匹配图片链接,然后逐个下载图片
  58.     html=BeautifulSoup(get_content(in_url),"html.parser")
  59.     n=0
  60.     pic_url=html.findAll("img",{"src":re.compile("http://img.symmz.com/img/.+\.jpg$")})
  61.     for each in pic_url:
  62.         print(each["src"])
  63.         n+=1
  64.         print("第%s张"%n)
  65.         pic_name="F://Python36//项目//图片//"+title+"//"+each["src"].split("/")[-1]
  66.         urllib.request.urlretrieve(each["src"],pic_name)

  67. def go():
  68.     #根据前面爬取到的标题和链接,创建文件夹,还有下载图片
  69.     for href,title in zip(href_list,title_list):
  70.         create_dir(title)
  71.         for num in range(1,11):
  72.             in_url="http://www.symmz.com"+href.split("-")[0]+"-"+str(num)+".html"
  73.             print(in_url)
  74.             download(in_url,title)

  75. def create_dir(title):
  76.     #创建文件夹
  77.     os.makedirs("F://Python36//项目//图片//"+title)
  78.    


  79. get_out_url()
  80. go()
复制代码

http://bbs.fishc.com/thread-102905-1-1.html 这个是写如何从西刺爬取代理并存入数据库的方法
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-10 14:18:38 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-10 18:57:59 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 19:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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