鱼C论坛

 找回密码
 立即注册
查看: 1935|回复: 4

[已解决]关于python爬虫的问题

[复制链接]
发表于 2017-9-1 21:38:34 | 显示全部楼层 |阅读模式

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

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

x
下面是我写的一段爬取京东手机图片的代码 程序没有报错 但是没有爬出东西 求大大帮忙看看 具体是正则用法的错误 还是其他的错误
  1. import re
  2. import urllib.request

  3. def craw(url,page):
  4.     html1 = urllib.request.urlopen(url).read()
  5.     html1 = str(html1)
  6.     pat1 = '<div id="J_goodsList".+?"page clearfix"'
  7.     result1 = re.compile(pat1).findall(html1)
  8.     result1 = result1[0]
  9.     pat2 =  '<img width="220" height="220" class="err-product" data-img="1" src="//img(.+?\.jpg)">'
  10.     photolist = re.compile(pat2).findall(result1)

  11.     x = 1
  12.     for photourl in photolist:
  13.         photoname = 'E:\\京东手机图片\\img1' + str(page) + str(x) + '.jpg'
  14.         photourl = 'http://' + photourl

  15.         try:
  16.             urllib.request.urlretrieve(photourl,filename = photoname)
  17.         except urllib.error.URLError as e:
  18.             if hasattr(e,'code'):
  19.                 x+=1
  20.             if hasattr(e,'reason'):
  21.                 x+=1
  22.         x+=1

  23. for i in range(1,50,2):
  24.     url = 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&page=' + str(i)
  25.     craw(url,i)
复制代码
最佳答案
2017-9-1 23:06:26
2017-09-01_230342.png

  1. import re
  2. import urllib.request

  3. def craw(url,page):
  4.     html1 = urllib.request.urlopen(url).read().decode('utf-8')
  5.     p =  r'<img width="220" height="220" class="err-product" data-img="1" src="(//.+\.jpg)" />'   
  6.     photolist = re.compile(p).findall(html1)

  7.     x = 1
  8.     for photourl in photolist:
  9.         photoname = 'E:\\京东手机图片\\img1\\' + str(page) + str(x) + '.jpg'
  10.         photourl = 'http:' + photourl
  11.         print(photourl)
  12.         try:
  13.             urllib.request.urlretrieve(photourl,filename = photoname)
  14.         except urllib.error.URLError as e:
  15.             if hasattr(e,'code'):
  16.                 x+=1
  17.             if hasattr(e,'reason'):
  18.                 x+=1
  19.         x+=1


  20. url = 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&page=1'
  21. craw(url,1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-1 23:06:26 | 显示全部楼层    本楼为最佳答案   
2017-09-01_230342.png

  1. import re
  2. import urllib.request

  3. def craw(url,page):
  4.     html1 = urllib.request.urlopen(url).read().decode('utf-8')
  5.     p =  r'<img width="220" height="220" class="err-product" data-img="1" src="(//.+\.jpg)" />'   
  6.     photolist = re.compile(p).findall(html1)

  7.     x = 1
  8.     for photourl in photolist:
  9.         photoname = 'E:\\京东手机图片\\img1\\' + str(page) + str(x) + '.jpg'
  10.         photourl = 'http:' + photourl
  11.         print(photourl)
  12.         try:
  13.             urllib.request.urlretrieve(photourl,filename = photoname)
  14.         except urllib.error.URLError as e:
  15.             if hasattr(e,'code'):
  16.                 x+=1
  17.             if hasattr(e,'reason'):
  18.                 x+=1
  19.         x+=1


  20. url = 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&page=1'
  21. craw(url,1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-2 00:28:40 | 显示全部楼层
建议你先去学习bs4比正则容易多了,后面慢慢研究正则
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-2 13:28:40 | 显示全部楼层
ド゛゜范 发表于 2017-9-2 00:28
建议你先去学习bs4比正则容易多了,后面慢慢研究正则

bs 和sc我没有安装 准备装一个  
昨天看了一天的正则 头痛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-4 20:42:06 | 显示全部楼层
丢丢yhj 发表于 2017-9-2 13:28
bs 和sc我没有安装 准备装一个  
昨天看了一天的正则 头痛

那就慢慢研究,肯定能懂的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-26 07:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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