0
我一直在尝试运行下面的代码,但它一直产生HTTP错误502.我认为错误的原因是因为网站知道程序是试图从中获取信息。因此,它不允许请求。有没有办法欺骗服务器认为这是一个合法的Web请求?我曾尝试添加标题,但它仍然无效。合法化Web请求,以便服务器允许请求通过
import urllib.request
# Function: Convert information within html document to a text file
# Append information to the file
def html_to_text(source_html, target_file):
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
print(source_html)
r = opener.open(source_html)
response = r.read()
print(response)
temp_file = open(target_file, 'w+')
temp_file.write(response.__str__())
source_address = "https://sg.finance.yahoo.com/lookup/all?s=*&t=A&m=SG&r=&b=0"
target_location = "C:\\Users\\Admin\\PycharmProjects\\TheLastPuff\\Source\\yahoo_ticker_symbols.txt"
html_to_text(source_address, target_location)
它适用于我。你向他们发送了多少请求?他们可能会将您的实验检测为bruteforce或dos攻击,并将您的请求的某些指纹列入黑名单 –
有没有办法欺骗服务器认为请求来自合法浏览器? – Cloud
是的。捕获浏览器发送的流量,并将标题值复制到您的Python脚本中。 –