V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Superbin
V2EX  ›  Python

百度搜索结果 url 爬虫 ,怎么实现爬指定 1 到 10 页的结果(现在是固定爬取某一页的 url)

  •  
  •   Superbin · Dec 6, 2017 · 3085 views
    This topic created in 3067 days ago, the information mentioned may be changed or developed.

    #coding=utf-8 import urllib2 import urllib import sys import re #from selenium import webdriver #from selenium.webdriver.common.keys import Keys import time

    #url = "href = "http://www.baidu.com/link?url=bu4fsa-txw7aHhz0LEu-Ej8ON__uS6btmV_mo7nI2O0_qKtfc-3rJHSyXnYOINHSgDASX4R1V6GcjE2UBGFdjZ9ahmEbG2gsGGW6MVW7pQm"" #print url pattern = re.compile(r"href = "( http://www.baidu.com/link?url=.+?)"") #rehh = re.findall(pattern, url)

    #for i in rehh: #print i

    with open('data.txt','a+') as f: key_word = [] with open('key_word.txt','r') as kf: for line in kf: request = urllib2.Request('http://www.baidu.com/s?wd='+line.decode('gbk').encode('utf-8')+'&pn=0') response = urllib2.urlopen(request)

            #print response.read()
            #pattern = re.compile(r"href = \"(.+?)\"")
            rehh = re.findall(pattern, response.read())
    
            for i in rehh:
                request2 = urllib2.Request(i)
                response2 = urllib2.urlopen(request2)
    
                print response2.geturl()
                f.write(response2.geturl())
                f.write('\n')
    

    f.close() kf.close()

    cyrbuzz
        1
    cyrbuzz  
       Dec 6, 2017
    排版感人。
    shawndev
        2
    shawndev  
       Dec 7, 2017
    selenium
    shawndev
        3
    shawndev  
       Dec 7, 2017
    pn=0,pn 即 pagenumber
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   820 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 20:21 · PVG 04:21 · LAX 13:21 · JFK 16:21
    ♥ Do have faith in what you're doing.