Python爬虫——4.6使用requests和正则表达式、随机代理爬取淘宝网商品信息
2018-01-15 09:13
941 查看
# coding:utf-8 ''' 使用requests模块,使用代理,进行淘宝网商品信息的爬取 ''' # 引入需要的模块 import requests import random import re from lxml import etree # 定义免费代理列表 proxy_list = [{"http": "116.8.83.3:8118"}, {"http": "116.8.83.3:8118"}, {"http": "113.89.59.161:8118"}, {"http": "113.67.183.196:8118"}, {"http": "180.155.135.224:31425"}, {"http": "123.161.153.238:22593"}] # 定义访问路由 url = 'https://uland.taobao.com/sem/tbsearch?refpid=mm_26632360_8858797_29866178&keyword=%E5%A5%B3%E8%A3%85&clk1=51ae9dffa6be64388c59f2c5c20274c7&upsid=51ae9dffa6be64388c59f2c5c20274c7' # 设置请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' } # 发送请求 response = requests.get(url, headers=headers, proxies=random.choice(proxy_list), verify=False) content = response.text # 图片名称的获取 reg_name = r'<span class="title" title=".*">(.*?)</span>' pattern_name = re.compile(reg_name) titles_list = pattern_name.findall(content) titles = [] for title in titles_list: titles.append(title) print "商品名称爬取结束" # 图片的获取 reg_name = r'<img data-ks-lazyload="(.*?)" s' pattern_name = re.compile(reg_name) imgs_list = pattern_name.findall(content) imgs = [] # 修改连接 for i in range(0, len(imgs_list) - 5): imgs_list[i] = "https:" + imgs_list[i] # 获取图片的url,进行第二次爬取 i = 1 for img in imgs_list: filename = str(i) + '_' + img[-20:] f = open('tb_img/%s' % filename, 'wb') url = img response = requests.get(url, verify=False) f.write(response.content) f.close() i += 1 print "图片爬取结束"
相关文章推荐
- Python爬取网页信息时,经常使用的正则表达式及方法
- Python 爬虫4——使用正则表达式筛选内容
- Python爬虫(十一)_案例:使用正则表达式的爬虫
- python 爬虫004-使用urllib2与正则表达式扒取糗事百科新鲜页首页帖子
- Python使用正则表达式获取网页中所需要的信息
- python爬虫里信息提取的核心方法: Beautifulsoup、Xpath和正则表达式
- Python3.7 爬虫(一)使用 Urllib2 与正则表达式抓取
- python爬虫学习---正则表达式的使用
- Python爬虫之正则表达式的使用(三)
- [python爬虫] 正则表达式使用技巧及爬取个人博客实例
- python基础学习——利用requests与re来动态爬取淘宝网商品信息
- Python 爬虫4——使用正则表达式筛选内容
- [python爬虫] 正则表达式使用技巧及爬取个人博客实例
- Python基础学习——正则表达式与第一个爬虫(requests)
- python 使用正则表达式的爬虫
- python爬虫提取信息:正则表达式和xpath
- python3实现网络爬虫(6)--正则表达式和BeautifulSoup配合使用
- python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解
- Python爬虫实践(七):正则表达式(2) re模块的使用
- python使用正则表达式编写网页小爬虫