python网络爬虫采集联想词示例

yipeiwu_com4年前Python爬虫

python爬虫_采集联想词代码

复制代码 代码如下:

#coding:utf-8
import urllib2
import urllib
import re
import time
from random import choice
#特别提示,下面这个list中的代理ip可能失效,请换上有效的代理ip
iplist  = ['27.24.158.153:81','46.209.70.74:8080','60.29.255.88:8888']

list1 = ["集团","科技"]
for item in list1:
    ip= choice(iplist)
    gjc = urllib.quote(item)
    url = "http://sug.so.360.cn/suggest/word?callback=suggest_so&encodein=utf-8&encodeout=utf-8&word="+gjc
    headers = {
                "GET":url,
                "Host":"sug.so.360.cn",
                "Referer":"http://www.so.com/",
                "User-Agent":"sMozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17",
                }

    proxy_support = urllib2.ProxyHandler({'http':'http://'+ip})

    opener = urllib2.build_opener(proxy_support)
    urllib2.install_opener( opener )
    req = urllib2.Request(url)

    for key in headers:
        req.add_header(key,headers[key])

    html = urllib2.urlopen(req).read()

    ss = re.findall("\"(.*?)\"",html)
    for item in ss:
        print item
    time.sleep(2)

相关文章

详解Python网络爬虫功能的基本写法

网络爬虫,即Web Spider,是一个很形象的名字。把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛。 1. 网络爬虫的定义 网络蜘蛛是通过网页的链接地址来寻找网页的...

Python爬取APP下载链接的实现方法

Python爬取APP下载链接的实现方法

首先是准备工作 Python 2.7.11:下载python Pycharm:下载Pycharm 其中python2和python3目前同步发行,我这里使用的是python2作为环境。P...

Python开发实例分享bt种子爬虫程序和种子解析

看到网上也有开源的代码,这不,我拿来进行了二次重写,呵呵,上代码:  #encoding: utf-8     &n...

Python常用的爬虫技巧总结

用python也差不多一年多了,python应用最多的场景还是web快速开发、爬虫、自动化运维:写过简单网站、写过自动发帖脚本、写过收发邮件脚本、写过简单验证码识别脚本。 爬虫在开发过程...

Python 爬虫爬取指定博客的所有文章

自上一篇文章 Z Story : Using Django with GAE Python 后台抓取多个网站的页面全文 后,大体的进度如下: 1.增加了Cron: 用来告诉程序每隔30分...