Simulate the browser -- Headers attribute and the headers attribute

Source: Internet
Author: User

Simulate the browser -- Headers attribute and the headers attribute

Method 1: Use build_opener () to modify the header

1 import urllib. request2 url = "https://www.cnblogs.com" 3 headers = (4 "User-Agent", 5 "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) chrome/64.0.3282.186 Safari/537.36 "6) 7 opener = urllib. request. build_opener () 8 opener. addheaders = [headers] 9 data = opener. open (url ). read ()

Method 2: Use add_header () to add a header

Import urllib. requesturl = "http://blog.csdn.net/column.html" req = urllib. request. Request (url) req. add_header (
"User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
) Data = urllib. request. urlopen (req). read ()

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.