python爬虫问题求解,为什么总是验证码错误? 75
#coding=utf-8frombs4importBeautifulSoupimporturllib2importsysimporttimeimportrequests...
#coding=utf-8
from bs4 import BeautifulSoup
import urllib2
import sys
import time
import requests
from PIL import Image
reload(sys)
sys.setdefaultencoding('utf-8')
time=time.localtime()
session=requests.session()
user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0'
headers={'User-Agent':user_agent}
cap_url='http://www.jsgsj.gov.cn:58888/mini/netWebServlet.json?randImg=true&tmp='+str(time)
html=session.get(cap_url,headers=headers)
img=html.content
cookies=html.cookies
with open('yanzhenma.jpeg','wb') as imgfile:
imgfile.write(img)
im = Image.open('yanzhenma.jpeg')
im.show()
yzm=raw_input()
url_company='http://www.jsgsj.gov.cn:58888/mini/netWebServlet.json?codeCheck=true&corpName=苏州&yzm='+str(yzm)
html=session.get(url=url_company,headers=headers,cookies=cookies)
page=(html.text)
print page 展开
from bs4 import BeautifulSoup
import urllib2
import sys
import time
import requests
from PIL import Image
reload(sys)
sys.setdefaultencoding('utf-8')
time=time.localtime()
session=requests.session()
user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0'
headers={'User-Agent':user_agent}
cap_url='http://www.jsgsj.gov.cn:58888/mini/netWebServlet.json?randImg=true&tmp='+str(time)
html=session.get(cap_url,headers=headers)
img=html.content
cookies=html.cookies
with open('yanzhenma.jpeg','wb') as imgfile:
imgfile.write(img)
im = Image.open('yanzhenma.jpeg')
im.show()
yzm=raw_input()
url_company='http://www.jsgsj.gov.cn:58888/mini/netWebServlet.json?codeCheck=true&corpName=苏州&yzm='+str(yzm)
html=session.get(url=url_company,headers=headers,cookies=cookies)
page=(html.text)
print page 展开
1个回答
展开全部
#把代码换一哈,验证码的参数名都不对 都发到百度上面来 神仙也帮不了你
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest', 'Cookie': 'verifycode={0};'.format(yzm)}
url_company='http://www.jsgsj.gov.cn:58888/mini/netWebServlet.json?codeCheck=true&corpName=苏州&verifycode='+str(yzm)
更多追问追答
追问
和headers没什么关系啊,我把headers全补充完整了,还是会出现验证码错误。而且验证码的参数,我用得fiddler看的,就是yzm。- -!
追答
我的代码运行结果呢...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询