问 题 1.此程序需要第三方模块 bs4 ,请先确保该模块已安装再实验。 2.爬虫网站为【http://tieba.baidu.com/p/2772...】,里面的楼主发了130张手机壁纸 3.问题参见代码里的注释 #-*-coding:utf-8 -*- from bs4 import BeautifulSoup import urllib def getContent(url): html=urllib.urlopen(url) content=html.read() html.close() return content info = getContent('http://tieba.baidu.com/p/2772656630') def getImages(info): soup=BeautifulSoup(info,'lxml') all_img=soup.find_all('img',class_='BD
以下是关于 爬虫图片 的编程技术问答
问 题 requests下载图片太麻烦了,虽然urlretrieve可以一步搞定,但是我想统一使用requests库。 注释部分就是用requests写的。 from urllib.request import urlretrieve import requests from bs4 import BeautifulSoup r = requests.get("http://www.pythonscraping.com") bs = BeautifulSoup(r.text) image = bs.find("a", {"id": "logo"}).find("img")["src"] urlretrieve(image, "logo.jpg") # ir = requests.get(image, stream=True) # if ir.status_code == 200: # with open('logo.jpg', 'wb') as f: #
问 题 我采集网页的时候,查看对方的图片链接是这样的: http://proxy.weixinla.com/http://mmbiz.qpic.cn/mmbiz/dVy7rPLbeRwMVBzbEMUiajT3TTT4EAjXibaImVS7CVnFicra0FwqYTHPZByr4aZl7awvicLyYqIh9XXsbwCZ9scFYA/640? 在浏览器可以正常访问,但是不能下载,我想把所有的图片都下载下来 要怎么处理呢? 解决方案 import urllib.request #导入request response = urllib.request.urlopen('http://proxy.weixinla.com/http://mmbiz.qpic.cn/mmbiz/dVy7rPLbeRwMVBzbEMUiajT3TTT4EAjXibaImVS7CVnFicra0FwqYTHPZByr4aZl7awvicLyYqIh9XXsbwCZ9scFYA/640?'
问 题 现在想爬取某个关键字下的所有商品的图片,例如关键字:电饭煲,大概要爬4400张图片。我试了一整天的pyspider,但是爬不了,请问用python还有其它方法可以做到吗? 解决方案 Python+PhantomJS+selenium 可以解决你的烦恼 参考这篇文章:http://blog.chinaunix.net/uid-22414998-id-3692113.html?page=3 测试代码: #coding=utf-8 from selenium import webdriver driver = webdriver.PhantomJS() driver.get("https://s.taobao.com/search?q=iphone") datas = driver.find_elements_by_class_name('J_ItemPic') for _ in datas: print _.get_attribute('s
问 题 import requests from bs4 import BeautifulSoup res2 = requests.get('http://news.sina.com.cn/c/2016-12-12/doc-ifxypcqa9432460.shtml') res.encoding = 'utf-8' soup= BeautifulSoup(res.text, 'html.parser') print(soup) 我这样为什么不能获得那个页面的正文部分,比如新闻的内容,标题,时间等。而只是获得上面的菜单栏。 解决方案 import requests from bs4 import BeautifulSoup res = requests.get('http://news.sina.com.cn/c/2016-12-12/doc-ifxypcqa9432460.shtml') res.encoding = 'utf-8' soup= BeautifulSoup(r
问 题 目前有一只爬虫,爬取的文章数量不是很大,但是以后会慢慢累加上去. 不过现在要做的一个是,在爬取的时候,需要先判断这个url有没有存在数据库,如果存在就不插入了. 我目前想到的思路是,每次采集的时候,都做一下判断: ( 查询一下就数据库 ) 但是这样感觉很低效.而且如果数据库里面有上百万的数据,每次都做一次判断那岂不是很慢!!? 有高效的方式吗? 解决方案 1. scrapy对request的URL去重 yield scrapy.Request(url, self.parse, dont_filter=False) 注意这个参数:dont_filter=False 2. Jobs: 暂停,恢复爬虫 启用一个爬虫的持久化,运行以下命令: scrapy crawl somespider -s JOBDIR=crawls/somespider-1 然后,你就能在任何时候安全地停止爬虫(按Ctrl-C或者发送一个信号)。 恢复
问 题 python利用urllib爬虫,图片获取二十几张后就报错 python版本3.6 windows系统下运行 urllib.error.URLError: 代码如下: #!/usr/bin/python # -*- coding:utf-8 -*- import urllib import requests import re from bs4 import BeautifulSoup import csv import socket socket.setdefaulttimeout(300) headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"} #url池 url1 =
这是报错信息: Traceback (most recent call last): File "D:\py\pic_downfrom2255ok.py", line 45, in html = getHtml(url_all[i]) File "D:\py\pic_downfrom2255ok.py", line 32, in getHtml html = response.read().decode() UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 184: invalid start byte 改了好多地方,主要可能是目标网站是gb2312编码, 这个程序在别的网站是可以正常下载图片的,换上现在的网站就有问题 还请各位多多指教,问题出在哪里?试了几个方法都不行 源码如下: #coding=utf-8 import urllib.r
今天模仿了Python社区里面的爬取王者荣耀的图片,很奇怪的是,我每爬取一个英雄的图片,pycharm就会给我报FileNotFoundError: [WinError 3] 的错。在代码中,我也给照片添加了循环,希望能够得到大神的帮助 #提取英雄名字和数字 hero_name = list(map(lambda x:x['cname'],html_json))#名字 hero_number = list(map(lambda x:x['ename'],html_json))#数字 #用于下载并保存图片 def main(): #list_name = list_of_name for i in range(18,100): for v in hero_number: os.mkdir("img/"+hero_name[i]) os.chdir("img/"+hero_name[i])
写了一小段代码,爬取博客园博客中的图片,这段代码对部分链接有效,还有一部分链接一爬就报错,这是什么原因呢? #coding=utf-8 import urllib import re from lxml import etree #解析地址 def getHtml(url): page = urllib.urlopen(url) html = page.read() return html #获取地址并建树 url = "http://www.cnblogs.com/fnng/archive/2013/05/20/3089816.html" html = getHtml(url) html = html.decode("utf-8") tree = etree.HTML(html) #保存图片至本地 reg = r'src="(.*?)" alt' imgre = re.compile(reg) imglist = re.findall(imgre,
问 题 问题描述: ValueError:unsupported format character 'j' (0x6a) at index 21 即倒数第6行 .jpg j出什么问题了??? import urllib.request from bs4 import BeautifulSoup def get_content(url): html = urllib.request.urlopen(url) content = html.read().decode('utf-8') html.close() return content def get_image(info): soup = BeautifulSoup(info,"lxml") all_image = soup.find_all('img',class_="BDE_Image") #一个一个保存 x=1 for image in all_image: print(image)