from bs4 import BeautifulSoup
import requests
import time
from lxml import etree
import os
# 该demo执行的为如何利用bs去爬一些文字
def start():
    # 发起网络请求
    html=requests.get('http://www.baidu')
    #编码
    html.encoding=html.apparent_encoding
    #创建sp
    soup=BeautifulSoup(html.text,'html.parser')
    print(type(soup))
    print('打印元素')
    print(soup.prettify())
    #存储一下title 该方法没有提示直接展示
    title=soup.head.title.string
    print(title)
#     写入文本
    with open(r'C:/Users/a/Desktop/a.txt','w') as f:
       f.write(title)
    print(time.localtime())

url_2 = 'http://news.gdzjdaily/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/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/zjxw/politics/'+next_page


get_html_from_etree(url_2)

if __name__ == '__main__':
    start()

镇楼吉祥物

更多推荐

python 爬虫如何执行自动下一页循环加载文字