猫史档案馆


爬虫开发技巧

用户:LaicalLaical查看:0 回复:1 评论:0 创建时间:2020-10-20T16:59:34


由于互联网的发展进步,网络爬虫也越来越多,更多的研发从事爬虫开发工作中,那在爬虫开发中需要掌握的技巧有那些呢?

1:GET请求和POST请求

image

2:伪装浏览器

现在很多网站为了防爬虫,都会对user-agent这个参数进行检测,我们在爬数据时把这个参数带上。

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW喵) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/70.0.1276.73 Safari/537.36', 'Referer':'https://www.喵/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=nike'} response = requests.get(url=url, headers=headers)
3:cookies处理

很多网站的数据都是需要你登录后才能进行数据爬取,而用户的登录状态则是记录在cookie中,这个时候我们就需要去模拟用户的登录来获取对应的cookie

API接口开始运行  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) Cookies生成进程开始运行 Cookies检测进程开始运行 正在生成Cookies 账号 180000000 密码 16yun 正在测试Cookies 用户名 180000000 Cookies有效 180000000
4:代理IP设置

通过代理IP的方式来解决对我们爬虫程序的限制,当我们发现我们的IP被封时,可以通过切换IP的形式来继续爬取数据

import org.json.JSONException; import org.json.JSONObject; import org.openqa.selenium.Platform; import org.openqa.selenium.Proxy; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; 
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider; import com.gargoylesoftware.htmlunit.WebClient; 
public class FirefoxDriverProxyDemo {  // 代理隧道验证信息  final static String proxyUser = "username";  final static String proxyPass = "password"; 
 // 代理服务器  final static String proxyHost = "t.16yun.cn";  final static int proxyPort = 31111; 
 final static String firefoxBin = "C:/Program Files/Mozilla Firefox/firefox.exe"; 
 public static void main(String[] args) throws JSONException  {  System.setProperty("webdriver.firefox.bin", firefoxBin); 
 FirefoxProfile profile = new FirefoxProfile(); 
 profile.setPreference("network.proxy.type", 1); 

 profile.setPreference("network.proxy.http", proxyHost);  profile.setPreference("network.proxy.http_port", proxyPort); 
 profile.setPreference("network.proxy.ssl", proxyHost);  profile.setPreference("network.proxy.ssl_port", proxyPort); 
 profile.setPreference("username", proxyUser);  profile.setPreference("password", proxyPass); 

 profile.setPreference("network.proxy.share_proxy_settings", true); 

 profile.setPreference("network.proxy.no_proxies_on", "localhost"); 

 FirefoxDriver driver = new FirefoxDriver(profile);  } } 

 


回复

上一页1 页 / 共 1下一页
小小爱html小小爱html

沙发

点赞0


评论