猫史档案馆


爬虫的方式搜搜狗网

用户:TimeLinkForeverTimeLinkForever查看:0 回复:9 评论:0 创建时间:2020-02-13T17:57:04


# encoding:gbk

import urllib.request, urllib.error, urllib.parse,sys
from bs4 import BeautifulSoup


def getsearch(a):
    output = ""

    if " " in a:
        a = a.split()
        for add in a:
            output += add
            output += '%20'
    else:
        output = a
    return output


回复

上一页1 页 / 共 1下一页
TimeLinkForeverTimeLinkForever

剩下的代码写不上来了,字数超过了,还不能频繁发帖,要的告诉我邮箱

点赞0


评论


TimeLinkForeverTimeLinkForever

def Search(Text, num):  
    header = {"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"}

    request = urllib.request.Request('https://www.sogou.com/web?

点赞0


评论


TimeLinkForeverTimeLinkForever

query=%s&page=%s'% (Text,num), headers = header)
    Myblabla = urllib.request.urlopen(request).read()
    print("ok")

    MySoup = BeautifulSoup(Myblabla,'html.parser',from_encoding = 'gbk')
    Links = MySoup.find_all('a')

点赞0


评论


TimeLinkForeverTimeLinkForever

    file = open("Final.cpp",'a')
    for Link in Links:
        try:
            if str(Link['href']).strip().startswith('/'):


                print('link:%s,txt:%s'% ('http://www.sogou.com' + Link['href'].strip()+"\n",str(Link.get_text()).strip()+"\n"))
            else:

点赞0


评论


TimeLinkForeverTimeLinkForever

print('link:%stxt:%s'% (Link['href'].strip()+"\n",str(Link.get_text()).strip()+"\n"))#.replace(u'\xa0', u'').encode('utf-8')
        except:

            print("Failed")
    file.close()
    return

点赞0


评论


TimeLinkForeverTimeLinkForever

print('link:%stxt:%s'% (Link['href'].strip()+"\n",str(Link.get_text()).strip()+"\n"))#.replace(u'\xa0', u'').encode('utf-8')
        except:

            print("Failed")
    file.close()
    return

print("Get Ready To Search!")

点赞0


评论


TimeLinkForeverTimeLinkForever

b = int(input("PageAmount>>>"))
a = input("PythonSeForSogou>>>")
hap = 1
for i in range(0,b):
    Search(getsearch(a),hap)
    hap+=1

点赞0


评论


TimeLinkForeverTimeLinkForever

自己整理一下顺序就好,字数太多网速慢只能这样

点赞0


评论


TimeLinkForeverTimeLinkForever

先看帖子内容,然后从下往上看

点赞0


评论