Urllib3 HTTP错误502:错误的网关

时间:2018-05-21 14:03:52

标签: python urllib3 bad-gateway

我正在尝试刮zk.fm以便下载音乐,但它给了我一些麻烦。我正在使用urllib3生成响应,但这总是会产生错误网关错误。通过浏览器访问网站非常合适。

这是我的代码(使用随机假用户代理)。我正在尝试访问“http://zk.fm/mp3/search?keywords=”,后跟一些表示歌曲名称和艺术家的关键字,例如“http://zk.fm/mp3/search?keywords=childish+gambino+heartbeat”。

from bs4 import BeautifulSoup
from random import choice
import urllib3 

desktop_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14',
             'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
             'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
             'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0']
def random_headers():
    return {'User-Agent': choice(desktop_agents)}

ua = random_headers()
http = urllib3.PoolManager(10,headers=user_agent)
response = http.request('GET',"http://zk.fm/mp3/search? 
keywords=childish+gambino+heartbeat")
soup = BeautifulSoup(response.data)

有没有办法解决502错误,还是我无法控制?

1 个答案:

答案 0 :(得分:1)

您需要启用Cookie的持久性,然后按顺序访问网站主页,然后访问搜索网址。我建议(个人)python请求,但这取决于你。请参阅here进行讨论。

我通过访问搜索页面测试了这个 - 错误502.访问主页 - 200.访问搜索 - 200.清除cookie并再次访问搜索 - 502.所以它必须是问题的cookie。