python 批量爬取代理ip

 
  
 1 import urllib.request
 2 import re
 3 import time
 4 import random
 5 
 6 
 7 def getResponse(url):
 8     req = urllib.request.Request(url)
 9     req.add_header("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36")
10     
11     resp = urllib.request.urlopen(req)
12     return resp.read()
13 
14 def getHtml(url,charSet = "utf-8"):
15     return getResponse(url).decode(charSet)
16     
17     
18 def createOpenner(ipList):
19     m_proxy = urllib.request.ProxyHandler({"http":random.choice(ipList)})
20     openner = urllib.request.build_opener(m_proxy)
21     urllib.request.install_opener(openner)
22     
23 #
24 def getProxyList(url,iPage = 10):
25     ipList = []
26     for i in range(1,iPage+1):
27         html_str = getHtml(url+str(i))
28         ip = re.findall("IP\">((?:\d{1,3}\.){3}(?:\d{1,3}))(?:[\s\S]{0,50})\"PORT\">(\d{2,4})", html_str)
29         for addr in ip:
30             ipList.append(addr[0]+":"+addr[1]) 
31         time.sleep(2)
32         
33     return ipList
34    
35  
36 ipList = getProxyList("http://www.kuaidaili.com/free/outha/",1)#爬取1页
37 
38 
39 print(ipList)

 

快代理:"IP\">((?:\d{1,3}\.){3}(?:\d{1,3}))(?:[\s\S]*?)\"PORT\">(\d{2,4})"     #下划线处原来是[\s\S]*,不带问号,后果是默认的贪婪模式。

只能取到一个地址,加上问号开启非贪婪模式。

 

西刺:"((?:\d{1,3}\.){3}(?:\d{1,3}))(?:[\s\S]*?)(\d{2,4})"

返回ipList列表:['46.101.3.126:8118', '177.207.234.14:80', '113.255.49.49:80', '52.59.18.222:80', '36.81.0.138:8080', '54.165.24.194:80', '115.252.35.104:8080', '136.169.58.21:8080', '51.254.106.65:80', '178.238.213.246:8080', '49.205.212.243:8080', '137.135.166.225:8131', '168.63.24.174:8138', '179.243.46.131:8080', '186.90.160.245:8080']

转载于:https://www.cnblogs.com/vawter/p/5907232.html

Python批量爬取淘宝商品价格数据,通常会使用到网络爬虫技术,比如使用requests库获取网页内容,然后解析HTML或JSON数据,利用BeautifulSoup、Scrapy等库帮助解析。以下是简单的步骤: 1. **安装必要的库**:首先需要安装`requests`, `beautifulsoup4`, 可能还需要`lxml`库,如果遇到JavaScript渲染的内容,可能需要`selenium`。 ```bash pip install requests beautifulsoup4 lxml (如果需要处理JS) selenium ``` 2. **分析目标网站结构**:访问淘宝商品页面,查看其HTML结构,找到包含商品价格的数据元素(例如CSS选择器或者XPath表达式)。 3. **编写爬虫脚本**:创建一个Python文件,如`tobao_scraper.py`,通过循环遍历链接列表并请求每个商品页,提取价格信息。 ```python import requests from bs4 import BeautifulSoup def get_price(url): response = requests.get(url) soup = BeautifulSoup(response.text, 'lxml') price_element = soup.select_one('.your-price-selector') # 将'.your-price-selector'替换为实际价格元素的选择器 return price_element.get_text() if price_element else None # 链接列表 urls = ['https://item.taobao.com/item.htm?id=your-item-id', ...] prices = [] for url in urls: price = get_price(url) if price: prices.append(price) # 打印或保存结果 for i, price in enumerate(prices): print(f"商品{i+1}的价格: {price}") ``` 注意:这只是一个基础示例,真实的淘宝商品页面可能会有反爬虫机制(如验证码、IP限制),并且频繁抓取可能会违反网站服务条款,因此在实际操作前应确保了解并遵守相关规定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值