python 新手求助

求求求,

python 编程 入门到实践,第十六章配套资源

展开阅读全文

Python 新手求助

08-31

[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/50.gif][/img]本人想做一个时时监控京东是否有货的程序,主要想练练手,但现在遇到了一个棘手的问题,望各位大大能帮忙解决,跪谢!!!rnrn出错页面rn[img=https://img-bbs.csdn.net/upload/201808/31/1535655831_388624.png][/img]rnrn代码写的乱,望谅解[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/2.gif][/img]rn[code=python]rnrn#!/usr/bin/python3rnrnrn#抓取页面代码rnfrom bs4 import BeautifulSouprnrnrnimport telnetlib #验证代理IPrnimport requestsrnimport winsound # 导入此模块实现声音播放功能rnimport time # 导入此模块,获取当前时间rnimport urllib.requestrnimport urllib.parsernimport randomrnrnrn#user_agent 集合rnuser_agent_list = [rnrn 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'rn]rnrn#代理地址rnproxy_url = 'http://api3.xiguadaili.com/ip/?tid=558635311593888&filter=on&num=1'rnrn#目标地址rntarget_url = 'https://item.jd.com/8945609.html#crumb-wrap'rnrnrndef user_proxy(proxy_addr, target_url):rn print('user_proxy 开始运行')rn #随机选择一个rn user_agent = random.choice(user_agent_list)rn headers = rn 'User-Agent':user_agent,rn 'Host':'chn.lottedfs.com',rn rn rn req = urllib.request.Request(url=target_url, headers=headers)rnrn #加载代理IPrn proxy = urllib.request.ProxyHandler('http': proxy_addr)rn opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)rn urllib.request.install_opener(opener)rn rn #try:rn data = urllib.request.urlopen(req,timeout=10).read().decode('utf-8')rn #print(data)rn #except Exception as e: rn # print('无法获取网站内容', str(e))rn #else:rn return datarnrnrnrndef check_proxy():rn print('check_proxy 开始运行')rn rn #随机选择一个rn user_agent = random.choice(user_agent_list)rn headers = rn 'User-Agent':user_agent, rn rn headers['Host'] = 'api3.xiguadaili.com'rn rn req1 = urllib.request.Request(url=proxy_url, headers=headers)rn rn rn #代理IP地址rn try: rn proxy_addr = urllib.request.urlopen(req1,timeout=5).read().decode('utf-8')rn except Exception as e:rn print('无法获取代理IP', str(e))rn time.sleep(5)rn check_proxy()rn else:rn ip_add = proxy_addr.split(':')rn print(proxy_addr)rn try:rn requests.get(target_url, proxies="http":proxy_addr, timeout=2)rn #telnetlib.Telnet(ip_add[0],port=ip_add[1],timeout=5)rn except Exception as e:rn print('该代理IP无效')rn time.sleep(5)rn check_proxy()rn else:rn print('该代理IP有效') rn return proxy_addrrn rnrnrnrnrni = 1rnrnrnwhile True:rn print('第',i,'次循环')rn print('---------------------------------------')rnrnrnrn #获取代理IPrn proxy_addr = check_proxy()rnrnrn #获网站内容rn try: rn data = user_proxy(proxy_addr, target_url)rnrn except Exception as e:rn print('无法获取网站内容', str(e))rn i = i+1rn #print('有错误,结束!')rn #breakrn continuern else:rn rn rn if data:rn soup = BeautifulSoup(data,'lxml')rn value = soup.select('[class="btn-special1 btn-lg"]')rn if value:rn print('------------------')rn print('*******有货*******')rn print('------------------')rn music = 'iphone.wav'rn winsound.PlaySound(music, winsound.SND_ALIAS) rn breakrn else:rn rn print('------------------')rn print('*******无货*******')rn print('------------------')rn i = i+1rn time.sleep(3)rn continue rn rn[/code] 论坛

没有更多推荐了,返回首页