Pyhton 链接重定向

问题描述 投票:0回答:2

我正在尝试使用 pyhton 从网站发送邮件。但是,在我要发送电子邮件的页面上,出现“找不到页面”警告。通过此警告后,我希望它带我返回链接并发送电子邮件。我如何提供此重定向? 通常我从数据库中提取连接。不过,我向您发送了一个链接作为示例。 我告诉它在内容为 200 时将我带到链接,但它没有,“请求未重定向”。我只想返回我指定的链接。我应用了类似问题的答案,但没有得到任何结果。

我想在不关闭驱动程序的情况下打开相同的链接

   ad_link = 'https://suchen.mobile.de/fahrzeuge/details.html?id=357845895&adLimitation=ONLY_FSBO_ADS&cn=DE&damageUnrepaired=NO_DAMAGE_UNREPAIRED&daysAfterCreation=1&grossPrice=true&isSearchRequest=true&makeModelVariant1.makeId=20100&makeModelVariant1.modelId=22&maxMile'
     headers = {
            'Accept': 'application/json',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 OPR/89.0.4447.64',
            #'Referer': 'https://suchen.mobile.de/fahrzeuge/search.html?dam=0&isSearchRequest=true&ref=quickSearch&sb=rel&vc=Car',
        }
        response = requests.get(ad_link, headers=headers,  allow_redirects=True)
        #, allow_redirects=True
        print(response.history)
        #print(ad_link)

        if response.history == 200:
            ic("Request was redirected")

            for resp in response.history:
                print(resp.status_code, resp.url)
            ic("Final destination:")
            ic(response.status_code, response.ad_link)
            ic("Mail Sending")
            test.implicitly_wait(3)
            test.find_element(By.XPATH,"/html/body/div[6]/div/div[2]/div[2]/div[2]/aside/div[1]/div/div[4]/div/span").click()
            test.implicitly_wait(1)
            eMl = test.find_element(By.XPATH,"/html/body/div[2]/div[1]/div[1]/div[2]/div/div[1]/form/div[1]/div/input")
            test.implicitly_wait(3)
            eMl.click()
            time.sleep(1)
            eMl.send_keys("[email protected]")
            time.sleep(3)
            passw = WebDriverWait(test, 5).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div[1]/div[1]/div[2]/div/div[1]/form/div[2]/div/input")))
            test.implicitly_wait(3)
            passw.click()
            time.sleep(1)
            test.find_element(By.XPATH, '/html/body/div/div[1]/div/div[2]/a').click()
            time.sleep(10)

        else:
            ic("Request was not redirected")
            ic(response)
        time.sleep(sleep_time)
python webdriver
2个回答
0
投票

const axios = require('axios');
const cheerio = require('cheerio');

let ad_link = 'https://suchen.mobile.de/fahrzeuge/details.html?id=357845895&adLimitation=ONLY_FSBO_ADS&cn=DE&damageUnrepaired=NO_DAMAGE_UNREPAIRED&daysAfterCreation=1&grossPrice=true&isSearchRequest=true&makeModelVariant1.makeId=20100&makeModelVariant1.modelId=22&maxMile';

let headers = {
    'Accept': 'application/json',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 OPR/89.0.4447.64'
}

async function sendEmail() {
    while (true) {
        try {
            let response = await axios.get(ad_link, {headers: headers});

            if (response.status == 200) {
                console.log("Request was redirected");

                // ... perform your actions here when request is successful ...
                break;
            } else {
                console.log("Request was not redirected");
            }
        } catch (error) {
            console.log("An error occurred while making the request:", error);
        }

        await new Promise(resolve => setTimeout(resolve, sleep_time));
    }
}
sendEmail();


0
投票

response.history
Response
对象的列表。您需要检查最后响应的状态码是否为200,以确定请求是否成功。

response.ad_link
不是有效属性。相反,您应该使用
response.url
来获取最终目标 URL。

import requests
import time
from icecream import ic

ad_link = 'https://suchen.mobile.de/fahrzeuge/details.html?id=357845895&adLimitation=ONLY_FSBO_ADS&cn=DE&damageUnrepaired=NO_DAMAGE_UNREPAIRED&daysAfterCreation=1&grossPrice=true&isSearchRequest=true&makeModelVariant1.makeId=20100&makeModelVariant1.modelId=22&maxMile'

headers = {
    'Accept': 'application/json',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 OPR/89.0.4447.64',
}

response = requests.get(ad_link, headers=headers, allow_redirects=True)

if response.status_code == 200:
    ic("Request was successful")

    for resp in response.history:
        print(resp.status_code, resp.url)

    ic("Final destination:")
    ic(response.status_code, response.url)

    ic("Mail Sending")
    # Your email sending code here

else:
    ic("Request was not successful")
    ic(response.status_code)
    ic(response.text)

time.sleep(sleep_time)
© www.soinside.com 2019 - 2024. All rights reserved.