网站首页可以访问,但包含广告的页面无法访问。抓取时如何绕过?

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

我正在尝试抓取一个网站的列表。

我遇到一个问题,我似乎无法通过脚本访问包含列表的页面,但主页可以正常访问。

import os
import random
import time
import requests

USER_AGENTS = []

with open('user-agents.txt', 'r') as file:
    USER_AGENTS = [line.strip() for line in file]

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'en-US,en;q=0.9',
    'Connection': 'keep-alive',
    'Cache-Control': 'max-age=0',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': random.choice(USER_AGENTS),
    'Referer': 'https://www.google.com/search?q=autoplius',
    'Origin': 'https://en.m.autoplius.lt',
    'DNT': '1',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-User': '?1',
    'Sec-Fetch-Dest': 'document'
}

def main():
    base_url = "https://en.m.autoplius.lt/"
    not_accessible = "https://en.m.autoplius.lt/ads/used-cars?qt="
    response = requests.get(not_accessible, headers=headers)
    if response.status_code != 200:
        print("Not today chap...")
        time.sleep(5)
        return False
    
    print("I got in!")
    return True

while True:
    main()

有没有办法改进发送的标头,以访问列表网站?还有哪些其他方式可以访问网站的其他页面?我还检查了robots.txt,它似乎没有对该网址路径的任何许可。

python web-scraping beautifulsoup web-crawler
1个回答
0
投票

我设法通过使用第三方代理提供商(又名“WEB Unblocker”)解决了这个问题。该网站似乎对机器人(脚本)有很强的保护,这导致我经常被识别为脚本。我通过不断地从更大的列表中更改用户标题,成功地获得了 200 个,但一段时间后,它们似乎仍然被识别为机器人。这是当前使用代理提供程序对我有用的代码:

import requests

proxies = {
  'http': 'http://USERNAME:[email protected]:PORT',
  'https': 'http://USERNAME:[email protected]:PORT',
}

response = requests.request(
    'GET',
    'https://autoplius.lt/skelbimai/naudoti-automobiliai',
    verify=False, 
    proxies=proxies,
)

print(response.status_code)
© www.soinside.com 2019 - 2024. All rights reserved.