最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python爬蟲(chóng)實(shí)現(xiàn)獲取下一頁(yè)代碼

 更新時(shí)間:2020年03月13日 14:43:19   作者:brady.wang  
在本篇文章里小編給大家整理了關(guān)于python爬蟲(chóng)實(shí)現(xiàn)獲取下一頁(yè)代碼內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。

我們首先來(lái)看下實(shí)例代碼:

from time import sleep

import faker
import requests
from lxml import etree

fake = faker.Faker()

base_url = "http://angelimg.spbeen.com"

def get_next_link(url):
  content = downloadHtml(url)
  html = etree.HTML(content)
  next_url = html.xpath("http://a[@class='ch next']/@href")
  if next_url:
    return base_url + next_url[0]
  else:
    return False

def downloadHtml(ur):
  user_agent = fake.user_agent()
  headers = {'User-Agent': user_agent,"Referer":"http://angelimg.spbeen.com/"}
  response = requests.get(url, headers=headers)
  return response.text

def getImgUrl(content):
  html = etree.HTML(content)
  img_url = html.xpath('//*[@id="content"]/a/img/@src')
  title = html.xpath(".//div['@class=article']/h2/text()")

  return img_url[0],title[0]

def saveImg(title,img_url):
  if img_url is not None and title is not None:
    with open("txt/"+str(title)+".jpg",'wb') as f:
      user_agent = fake.user_agent()
      headers = {'User-Agent': user_agent,"Referer":"http://angelimg.spbeen.com/"}
      content = requests.get(img_url, headers=headers)
      #request_view(content)
      f.write(content.content)
      f.close()

def request_view(response):
  import webbrowser
  request_url = response.url
  base_url = '<head><base href="%s" rel="external nofollow" >' %(request_url)
  base_url = base_url.encode()
  content = response.content.replace(b"<head>",base_url)
  tem_html = open('tmp.html','wb')
  tem_html.write(content)
  tem_html.close()
  webbrowser.open_new_tab('tmp.html')

def crawl_img(url):
  content = downloadHtml(url)
  res = getImgUrl(content)
  title = res[1]
  img_url = res[0]
  saveImg(title,img_url)

if __name__ == "__main__":
  url = "http://angelimg.spbeen.com/ang/4968/1"

  while url:
    print(url)
    crawl_img(url)
    url = get_next_link(url)

python 爬蟲(chóng)如何執(zhí)行自動(dòng)下一頁(yè)循環(huán)加載文字

from bs4 import BeautifulSoup
import requests
import time
from lxml import etree
import os
# 該demo執(zhí)行的為如何利用bs去爬一些文字
def start():
  # 發(fā)起網(wǎng)絡(luò)請(qǐng)求
  html=requests.get('http://www.baidu.com')
  #編碼
  html.encoding=html.apparent_encoding
  #創(chuàng)建sp
  soup=BeautifulSoup(html.text,'html.parser')
  print(type(soup))
  print('打印元素')
  print(soup.prettify())
  #存儲(chǔ)一下title 該方法沒(méi)有提示直接展示
  title=soup.head.title.string
  print(title)
#   寫(xiě)入文本
  with open(r'C:/Users/a/Desktop/a.txt','w') as f:
    f.write(title)
  print(time.localtime())
 
url_2 = 'http://news.gdzjdaily.com.cn/zjxw/politics/sz_4.shtml'
def get_html_from_bs4(url):
 
  # response = requests.get(url,headers=data,proxies=ip).content.decode('utf-8')
  response = requests.get(url).content.decode('utf-8')
  soup = BeautifulSoup(response, 'html.parser')
  next_page = soup.select('#displaypagenum a:nth-of-type(9)')[0].get('href')
  # for i in nett
  print(next_page)
  next2='http://news.gdzjdaily.com.cn/zjxw/politics/'+next_page
 
 
def get_html_from_etree(url):
 
  response = requests.get(url).content.decode('utf-8')
  html= etree.HTML(response)
 
  next_page = html.xpath('.//a[@class="PageNum"][8]/@href')[0]
  print(next_page)
  # next2='http://news.gdzjdaily.com.cn/zjxw/politics/'+next_page
 
 
get_html_from_etree(url_2)
 
if __name__ == '__main__':
  start()

到此這篇關(guān)于python爬蟲(chóng)實(shí)現(xiàn)獲取下一頁(yè)代碼的文章就介紹到這了,更多相關(guān)python爬蟲(chóng)獲取下一頁(yè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

诏安县| 丹凤县| 呈贡县| 奎屯市| 乳山市| 同仁县| 东源县| 吉安县| 贺州市| 灯塔市| 璧山县| 且末县| 建平县| 通州区| 永平县| 山东省| 张家川| 木兰县| 集安市| 化德县| 科尔| 吉水县| 墨竹工卡县| 莱芜市| 兴海县| 安陆市| 寿阳县| 石景山区| 松原市| 上饶县| 肇州县| 通化县| 台中市| 太保市| 元谋县| 巩留县| 双江| 广州市| 桃园市| 贵阳市| 普安县|