爬虫:一个url多页

import urllib.request
import urllib.parse

from lxml import etree

#链接

url = 'http://www'

#循环得到分页

for i in range(1,26):

#查到到页数关键词

    query = {'Page':'i'}
    data = urllib.parse.urlencode(query).encode(encoding = 'utf8')
    post = urllib.request.Request(url,data)

    response = urllib.request.urlopen(post)

#获得源代码

    page = response.read()

    s = etree.HTML(page)

#查找需到的信息

    file = s.xpath('//a[@class="link-01"][1]/text()')

    print(file)

走了很多坑,python3.x和python2.x有些不同,2.x有urllib、urllib2,3.x只有urllib:

  • 在Pytho2.x中使用import urllib2——-对应的,在Python3.x中会使用import urllib.requesturllib.error
  • 在Pytho2.x中使用import urllib——-对应的,在Python3.x中会使用import urllib.requesturllib.errorurllib.parse
  • 在Pytho2.x中使用import urlparse——-对应的,在Python3.x中会使用import urllib.parse
  • 在Pytho2.x中使用import urlopen——-对应的,在Python3.x中会使用import urllib.request.urlopen
  • 在Pytho2.x中使用import urlencode——-对应的,在Python3.x中会使用import urllib.parse.urlencode
  • 在Pytho2.x中使用import urllib.quote——-对应的,在Python3.x中会使用import urllib.request.quote
  • 在Pytho2.x中使用cookielib.CookieJar——-对应的,在Python3.x中会使用http.CookieJar
  • 在Pytho2.x中使用urllib2.Request——-对应的,在Python3.x中会使用urllib.request.Request

猜你喜欢

转载自blog.csdn.net/z13405546523/article/details/80112840