


Python implements sharing of methods for crawlers to set proxy IP and disguise themselves as browsers
This article mainly introduces the sharing of methods for Python to implement crawler setting proxy IP and disguise as a browser. It has a certain reference value. Now it is shared with everyone. Friends in need can refer to it
1.python crawler browser disguise
#导入urllib.request模块 import urllib.request #设置请求头 headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0") #创建一个opener opener=urllib.request.build_opener() #将headers添加到opener中 opener.addheaders=[headers] #将opener安装为全局 urllib.request.install_opener(opener) #用urlopen打开网页 data=urllib.request.urlopen(url).read().decode('utf-8','ignore')
2.Set proxy
#定义代理ip proxy_addr="122.241.72.191:808" #设置代理 proxy=urllib.request.ProxyHandle({'http':proxy_addr}) #创建一个opener opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle) #将opener安装为全局 urllib.request.install_opener(opener) #用urlopen打开网页 data=urllib.request.urlopen(url).read().decode('utf-8','ignore')
3. Set up proxy and simulated browser access at the same time
#定义代理ip proxy_addr="122.241.72.191:808" #创建一个请求 req=urllib.request.Request(url) #添加headers req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) #设置代理 proxy=urllib.request.ProxyHandle("http":proxy_addr) #创建一个opener opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle) #将opener安装为全局 urllib.request.install_opener(opener) #用urlopen打开网页 data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
4. Add multiple information in the request header
import urllib.request page_headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0", "Host":"www.baidu.com", "Cookie":"xxxxxxxx" } req=urllib.request.Request(url,headers=page_headers) data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
5. Add post request parameters
import urllib.request import urllib.parse #设置post参数 page_data=urllib.parse.urlencode([ ('pn',page_num), ('kd',keywords) ]) #设置headers page_headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0', 'Connection':'keep-alive', 'Host':'www.lagou.com', 'Origin':'https://www.lagou.com', 'Cookie':'JSESSIONID=ABAAABAABEEAAJA8F28C00A88DC4D771796BB5C6FFA2DDA; user_trace_token=20170715131136-d58c1f22f6434e9992fc0b35819a572b', 'Accept':'application/json, text/javascript, */*; q=0.01', 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', 'Referer':'https://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98?labelWords=&fromSearch=true&suginput=', 'X-Anit-Forge-Token':'None', 'X-Requested-With':'XMLHttpRequest' } #打开网页 req=urllib.request.Request(url,headers=page_headers) data=urllib.request.urlopen(req,data=page_data.encode('utf-8')).read().decode('utf-8')
6. Use phantomjs to simulate browser requests
##
#1.下载phantomjs安装到本地,并设置环境变量 from selenium import webdriver bs=webdriver.PhantomJS() #打开url bs.get(url) #获取网页源码 url_data=bs.page_source #将浏览到的网页保存为图片 bs.get_screenshot_as_file(filename)
7.phantomjs sets user-agent and cookie
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities dcap = dict(DesiredCapabilities.PHANTOMJS) dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0") bs = webdriver.PhantomJS(desired_capabilities=dcap) bs.get(url) #删除cookie bs.delete_all_cookies() #设置cookie #cookie格式:在浏览器cookie中查看,一个cookie需要包含以下参数,domain、name、value、path cookie={ 'domain':'.www.baidu.com', #注意前面有. 'name':'xxxx', 'value':'xxxx', 'path':'xxxx' } #向phantomjs中添加cookie bs.add_cookie(cookie)
8.Use the web_driver tool
#1.下载web_driver工具(如chromdriver.exe)及对应的浏览器 #2.将chromdriver.exe放到某个目录,如c:\chromdriver.exe from selenium import webdriver driver=webdriver.Chrome(executable_path="C:\chromdriver.exe") #打开url driver.get(url)
Python video crawler implements downloading headline videos
##Instance of Python crawler grabbing proxy IP and checking availabilityPython method to collect proxy IP and determine whether it is available and update regularlyThe above is the detailed content of Python implements sharing of methods for crawlers to set proxy IP and disguise themselves as browsers. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
