【问题标题】:bs4 and requests proxy script errorbs4 和请求代理脚本错误
【发布时间】:2018-08-26 19:04:25
【问题描述】:

我目前正在编写一个从 Supremenewyork.com 提取信息的脚本 我使用的这个代理脚本之前一直在“排序”,现在它根本不起作用,因为我在我的电脑上看到了这个叫做 urllib3 的东西,我认为它没用,所以我卸载了它,然后我尝试再次运行我的代理脚本,我得到一个关于 urllib3 的错误,所以我很快重新安装了 urllib3,但在那之后我的脚本再也没有工作过...... 这是我的脚本:

import requests
from bs4 import BeautifulSoup
UK_Proxy1 = input('UK http Proxy1: ')
UK_Proxy2 = input('UK http Proxy2: ')

proxies = {
 'http': 'http://' + UK_Proxy1 + '',
   'https': 'http://' + UK_Proxy2 + '',

}

categorys = ['jackets','shirts','tops_sweaters','sweatshirts','pants','shorts','t-shirts','hats','bags','accessories','shoes','skate']
catNumb = 0

for cat in categorys:
    catStr = str(categorys[catNumb])
    cUrl = 'http://www.supremenewyork.com/shop/all/' + catStr
    proxy_script = requests.get(cUrl, proxies=proxies).text
    bSoup = BeautifulSoup(proxy_script, 'lxml')
    print('\n*******************"'+ catStr.upper() + '"*******************\n')
catNumb += 1
for item in bSoup.find_all('div', class_='inner-article'):
    url = item.a['href']
    alt = item.find('img')['alt']
    req = requests.get('http://www.supremenewyork.com' + url)
    item_soup = BeautifulSoup(req.text, 'lxml')
    name = item_soup.find('h1', itemprop='name').text
    style = item_soup.find('p', itemprop='model').text
    print (alt + ' --- ' + name + ' --- ' + style)

当我运行此脚本并输入英国代理时,我收到此错误

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/util/connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

在处理上述异常的过程中,又发生了一个异常:

  Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/util/connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

在处理上述异常的过程中,又发生了一个异常:

`Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connection.py", line 166, in connect
    conn = self._new_conn()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connection.py", line 150, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x112d10eb8>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known

During handling of the above exception, another exception occurred: (same error as above and continues for a bit)

 File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib3/util/retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='109.108.153.29\t', port=80): Max retries exceeded with url: http://www.supremenewyork.com/shop/all/jackets (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x112d10eb8>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)))

我尝试了几种不同的代理,但都没有奏效 有人可以帮助我吗,我真的很感激它

【问题讨论】:

    标签: python-3.x proxy beautifulsoup python-requests urllib3


    【解决方案1】:

    这里是答案的基线, 它不起作用,因为没有连接到代理。

    你所要做的就是为它提供一个工作代理和一个端口,如果你的计算机没有互联网连接,它会给你同样的错误,但因为你在 StackOverflow 我会假设你有那个。

    urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='109.108.153.29\t', port=80): Max retries exceeded with url: http://www.supremenewyork.com/shop/all/jackets (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x112d10eb8>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))) 以下是我们修复它的方法: 我们不使用有效的代理并为其提供端口, 您可以在此网站上获取代理列表并使用random.choice() 始终选择其他代理。http://www.gatherproxy.com/

    import requests
    from bs4 import BeautifulSoup
    UK_Proxy1 = '173.212.202.65:80'
    # UK_Proxy2 = input('UK http Proxy2: ')
    
    proxies = {
     'http': 'http://' + UK_Proxy1,
       'https': 'https://' + UK_Proxy1
    }
    
    categorys = ['jackets','shirts','tops_sweaters','sweatshirts','pants','shorts','t-shirts','hats','bags','accessories','shoes','skate']
    catNumb = 0
    
    for cat in categorys:
        catStr = str(categorys[catNumb])
        cUrl = 'http://www.supremenewyork.com/shop/all/' + catStr
        proxy_script = requests.get(cUrl, proxies=proxies).text
        bSoup = BeautifulSoup(proxy_script, 'lxml')
        print('\n*******************"'+ catStr.upper() + '"*******************\n')
    catNumb += 1
    for item in bSoup.find_all('div', class_='inner-article'):
        url = item.a['href']
        alt = item.find('img')['alt']
        req = requests.get('http://www.supremenewyork.com' + url)
        item_soup = BeautifulSoup(req.text, 'lxml')
        name = item_soup.find('h1', itemprop='name').text
        style = item_soup.find('p', itemprop='model').text
        print (alt + ' --- ' + name + ' --- ' + style)
    

    【讨论】:

    • 感谢您的回答!所以基本上我会使用另一个 bs4 请求脚本将代理 + 端口一起拉到 UK_Proxy1 中?
    • @Elivir Muslic 我已经通过输入 ip 地址进行了多次测试:端口大多数时候我都会遇到同样的错误,但有时我会通过它能够提取信息来得到我想要的从最高纽约的 DIFF 网站上,文本被打乱了,我猜它不再是文本,所以我得到了 proxies=proxies).text 事物的文本这样的属性
    • @bobo 乱码可能是因为代理无效(即有些代理只用于宣传他们的产品恶意) 您可以从此网站或其他网站(已更新)或提供您自己的网站。
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    相关资源
    最近更新 更多