新闻  |   论坛  |   博客  |   在线研讨会
爬虫开发技巧
laical | 2020-10-20 17:08:56    阅读:89   发布文章

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

1:GET请求和POST请求

2:伪装浏览器

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


headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.1276.73 Safari/537.36', 'Referer':'https://www.baidu.com/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);    } }              

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客