作家
登录

Python利用Beautifulsoup爬取笑话网站

作者: 来源: 2017-09-19 19:07:54 阅读 我要评论

  •  
  • headers = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
  • all_html=requests.get(all_url,headers = headers) 
  • #print(all_html.text) 
  • soup1 = BeautifulSoup(all_html.text,'lxml'
  • list1=soup1.find_all('li',class_ = 'article-summary'
  • for i in list1: 
  •     #print(i) 
  •     soup2 = BeautifulSoup(i.prettify(),'lxml'
  •     list2=soup2.find_all('a',target = '_blank',class_='all-read'
  •     for b in list2: 
  •         href = b['href'
  •         print(href)  
  • 也就是说,我们只须要获得所有的轮回遍历所有的页码,就能获得所有的笑话。

    膳绫擎的代码优化后:

    1. from bs4 import BeautifulSoup 
    2. import os 
    3. import requests 
    4. all_url = 'http://xiaohua.zol.com.cn/new/5.html  
    5.  
    6. def Gethref(url): 
    7.     headers = { 'User-Agent'"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
    8.     html = requests.get(url,headers = headers) 
    9.     soup_first = BeautifulSoup(html.text,'lxml'
    10.     list_first = soup_first.find_all('li',class_='article-summary'
    11.     for i in list_first: 
    12.         soup_second = BeautifulSoup(i.prettify(),'lxml'
    13.         list_second = soup_second.find_all('a',target = '_blank',class_='all-read'
    14.         for b in list_second: 
    15.             href = b['href'
    16.             print(href) 
    17. Gethref(all_url)  

    应用如下代码,获取完全的笑话地址url

    1. from bs4 import BeautifulSoup 

        推荐阅读

        Java并发编程之并发代码设计

      【沙龙】51CTO诚邀您9月23号和多位技巧大年夜咖一路聊智能CDN的优化之路,抓紧时光哦! 引子之前的文┞仿我们商量了激发线程安然的原因主如果因为多线程的对共享内存的操作导致的可见性或有>>>详细阅读


      本文标题:Python利用Beautifulsoup爬取笑话网站

      地址:http://www.17bianji.com/lsqh/37529.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)