基于搜狗接口的微信公众号及其信息爬取

1.思路

经测试,搜狗搜索提供的微信公众号的接口是理所当然爬取多了会被封ip等方式重点照顾。这只是做一个公众号及其链接的爬取,公众号的内容爬取及制定内容的爬取都是一个路子。搞懂了一个其他的就都差不多了。

2.整体实现

    具体实现的话就两个方面:

    1)代理的使用(获取可用ip)

    2)微信公众号的爬取

3.代码部分

1)获取可用的ip,然后随机选取一个ip

 def get_ip_list(self):                     #从西刺代理网站上获取一个IP列表 
        url = 'http://www.xicidaili.com/nn/'
        web_data = requests.get(url, headers=self.headers)
        soup = BeautifulSoup(web_data.text, 'lxml')
        ips = soup.find_all('tr')
        ip_list = []
        for i in range(1, len(ips)):
            ip_info = ips[i]
            tds = ip_info.find_all('td')
            ip_list.append(tds[1].text + ':' + tds[2].text)
        return ip_list
    def get_random_ip(self,ip_list):
        proxy_list = []
        for ip in ip_list:
            proxy_list.append('http://' + ip)
        proxy_ip = random.choice(proxy_list)
        proxies = {'http': proxy_ip}
        print(proxies)
        return proxies

2)代理的使用

httpproxy_handler = urllib.request.ProxyHandler(proxy)
proxy_handler = urllib.request.ProxyHandler(httpproxy_handler)
#httpproxy_handler是形如{'http': 'http://121.193.143.249:80/'}这样的字典类型的数据)
opener = urllib.request.build_opener(proxy_handler)
r = opener.open('http://httpbin.org/ip')
print(r.read())

代理使用模板 ↑ ↑ ↑ ↑ ↑

3)主函数

    def start(self,url,proxy,n):    #参数n是为了,当有ip地址不能使用时产生下一个ip而继续爬取当前页面
        try:
            httpproxy_handler = urllib.request.ProxyHandler(proxy)
            opener = urllib.request.build_opener(httpproxy_handler)     #使用代理ip
            request=urllib.request.Request(url,headers=self.headers)
            response = opener.open(request)
            data=response.read()
            # print(data)
            datahref=[]                #定义变量
            title_all=[]                    #存放标题
            soup = BeautifulSoup(data, 'html.parser', from_encoding='utf-8')   #做汤
            links=soup.find_all('div',class_='img-box')    #主要内容提取
            for link in links:
                datahref.append(link.a['href'])
                # print(link.a['href'])
            file=open('link.text','a')          #链接写到link文档中

            for i in range(0,len(datahref)):
                link_ultimate=datahref[i]
                print(datahref[i])
                name=self.getname(link_ultimate)
                title_all.append(name)
                file.write(name+link_ultimate)
            n=n+1
        except Exception as e:
            print(e)
        return n

4)获取公众号名称:
这个地方的变种操作:可以获取公众号的描述,账号,信息等等各种东西。

    def getname(self, url):  # 获取每一个公众号的名称
        try:
            request = urllib.request.Request(url, headers=self.headers)
            response = urllib.request.urlopen(request)
            data = response.read()
            soup = BeautifulSoup(data, 'html.parser', from_encoding='utf-8')
            tag = soup.strong
            name = tag.contents[0].replace(" ","").replace("\n",'')         #用replace方法
            # print(tag.contents[0])
            print(name)
            # print(tag.contents)
        except Exception as e:
            print(e)
        return name

4.日常总结

这是一个闲着无聊的操作,上次也和班主任有交流过我想做一个整套东西的这个想法。
思考1:做这个东西的研究价值和经济意义在哪里?

如果以后做毕业设计的话一套用砖头堆砌起来的墙是没什么太大的意义的,必须是有其中可研究的亮点,经济价值或者为大家服务的一套系统。

思考2:写爬虫的基本路子。

1.合理分析定义所需要的变量
2.做汤
3.具体网页内容刨析,re或者美丽的汤中的find方法
4.爬取出来的内容如何保存

思考3:如何使用代理。

httpproxy_handler = urllib.request.ProxyHandler(proxy)
proxy_handler = urllib.request.ProxyHandler(httpproxy_handler)
#httpproxy_handler是形如{'http': 'http://121.193.143.249:80/'}这样的字典类型的数据)
opener = urllib.request.build_opener(proxy_handler)
r = opener.open('http://httpbin.org/ip')
print(r.read())

思考4:关于人生的一点思考。

老生常谈的一个,做事情要有决心。
不要畏惧,不会的东西就多花点时间放轻松去做
克服学习焦虑症,对未来充满希望,不要再惧怕什么
有想法多向班主任请教,不会的硬着头皮问别人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值