简体   繁体   中英

Syntax Error in Python Requests library for the identifier “headers”

I'm trying to build a webscraper from a tutorial I watched. Replicating the same work is giving me the following error.

import requests
import bs4
r = requests.get("http://www.pyclass.com/example.html", headers={"User-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"})  
c=r.content

The error says "Syntax Error: Invalid character in identifier" The word headers is being highlighted.

I really need to use headers so that I can fetch the data by impersonating a web browser, otherwise I am getting a 406 error without it.

Try below code.

import requests
head={"User-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"}
r=requests.get("http://www.example.com/", headers=head)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM