【问题标题】:Python Flask web scraping error when use proxy使用代理时 Python Flask Web 抓取错误
【发布时间】:2019-10-14 22:36:19
【问题描述】:

我使用 Imgur 在土耳其被禁止的代理从 Imgur 获取照片网址。我仍然收到 requests.exceptions.ConnectionError。我该怎么办?

from flask import Flask, render_template
from bs4 import BeautifulSoup
import requests

headers = {
    "User-Agent": 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0'}
proxies = {
    'http': '104.32.43.13:53281',
    'https': '104.32.43.13:53281',
    }
app = Flask(__name__)
@app.route('/')
def index():
    source = requests.get('https://imgur.com/t/fall/WuL2IqJ',proxies=proxies, headers=headers)
    soup = BeautifulSoup(source.content, 'html.parser')
    url = soup.find('img',class_='post-image-placeholder')['src']
    urlT = url.text
    return render_template('home.html',**locals())
app.run(debug=True)

【问题讨论】:

  • 代理可能已死
  • @MohitC 我尝试了 30 个代理,它们都给出了相同的错误

标签: python proxy python-requests


【解决方案1】:

尽管像 IP:PORTUSER:PASS:IP:PORT 这样传递代理很常见,但您需要重新格式化这些代理,以便请求了解如何连接:

https://requests.kennethreitz.org/en/master/user/advanced/#proxies

您的代理字典应该类似于:

proxies={
    'http': 'http://0.0.0.0:0000',
    'https': 'https://0.0.0.0:0000'
}

请注意,如果您有用户通过身份验证的代理,则需要将它们重新格式化为:proxies = {'http': 'http://user:pass@10.10.1.10:3128/'}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 2020-07-30
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多