Light
Light

Reputation: 41

request returns response 412

I'm trying to access a Chinese website(http://www.nhc.gov.cn/wjw/gfxwjj/list.shtml) with python, but it returns the request status code [412]

HEADERS = ({'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
            'Accept-Language': 'ja, en-US;q=0.9, en;q=0.8'})
 
url = 'http://www.nhc.gov.cn/wjw/gfxwjj/list.shtml'
res = requests.get(url, headers=HEADERS)
print(res)

Does anyone know how to fix it? I can access the website with normal browsers. And I can also get a status code[200] when I access the other website with python. Thanks.

Upvotes: 0

Views: 1016

Answers (1)

Light
Light

Reputation: 41

I could solved this problem by myself. The problem was caused because of the Navigator.webdriver was "TRUE". Some websites block the access from the webdriver.

To solve this, I use selenium with chromedriver and set the Navigator.webdriver "undefined". The exact code to do this is below,

options = Options()
options.add_argument("--disable-blink-features")
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(
    executable_path='your chromedriver's path', chrome_options=options)

Thank you for everyone who tried to solve my problem. I hope it'll help someone who has the same problem.

Upvotes: 3

Related Questions