一般的抓取网页的使用 beautifulsoup就足够了,只有做复杂的爬虫类的应用才需要用到PySpider等类型的框架。下面给出直接用python源码写的抓取新浪爱彩双色球开奖数据的代码。
    image.png

    1. import urllib.request
    2. import urllib.parse
    3. import http.cookiejar
    4. def getHtml(url):
    5. cj = http.cookiejar.CookieJar()
    6. opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    7. opener.addheaders = [('User-Agent',
    8. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'),
    9. ('Cookie', '4564564564564564565646540')]
    10. urllib.request.install_opener(opener)
    11. html_bytes = urllib.request.urlopen(url).read()
    12. html_string = html_bytes.decode('utf-8')
    13. return html_string
    14. # 获取网页的HTML内容
    15. html = getHtml("http://zst.aicai.com/ssq/openInfo/")
    16. # 开奖信息在表格中,获取表格<table class="fzTab nbt"></table>中的数据
    17. table = html[html.find('<table class="fzTab nbt">'): html.find('</table>')]
    18. print(table) # 查看获取到的表格中的数据
    1. table = html[html.find('<table class="fzTab nbt">'): html.find('</table>')]

    image.png

    1. # 根据'<tr \r\n\t\t onmouseout=' 将得到的字符串切分为列表
    2. tmp = table.split('<tr \r\n\t\t onmouseout=')
    3. print(tmp[1])
    1. "this.style.background=''"
    2. onmouseover="this.style.background='#fff7d8'">
    3. <td >2020134</td>
    4. <td >2020-12-31</td>
    5. <td class="redColor sz12" >02</td>
    6. <td class="redColor sz12" >09</td>
    7. <td class="redColor sz12" >10</td>
    8. <td class="redColor sz12" >20</td>
    9. <td class="redColor sz12" >22</td>
    10. <td class="redColor sz12" >26</td>
    11. <td class="blueColor sz12" >01</td>
    12. <td >399,565,532</td>
    13. <td class="redColor sz12" >48</td>
    14. <td >5,382,786</td>
    15. <td >275</td>
    16. <td >83,517</td>
    17. <td >704,501,324</td>
    18. </tr>
    19. <tr
    20. class="camBlue" onmouseout="this.style.background='#eff3f7'"
    21. onmouseover="this.style.background='#fff7d8'">
    22. <td >2020133</td>
    23. <td >2020-12-29</td>
    24. <td class="redColor sz12" >03</td>
    25. <td class="redColor sz12" >19</td>
    26. <td class="redColor sz12" >22</td>
    27. <td class="redColor sz12" >23</td>
    28. <td class="redColor sz12" >27</td>
    29. <td class="redColor sz12" >29</td>
    30. <td class="blueColor sz12" >07</td>
    31. <td >370,330,222</td>
    32. <td class="redColor sz12" >3</td>
    33. <td >10,000,000</td>
    34. <td >99</td>
    35. <td >283,877</td>
    36. <td >893,973,468</td>
    37. </tr>
    # 获取</tr>以前的第一行数据
    tr = tmp[: tmp.find('</tr>')]
    print(tr)
    
    "this.style.background=''"
                              onmouseover="this.style.background='#fff7d8'">
    
                              <td   >2020117</td>
                              <td   >2020-11-22</td>
                              <td  class="redColor sz12" >07</td>
                              <td  class="redColor sz12" >08</td>
                              <td  class="redColor sz12" >09</td>
                              <td  class="redColor sz12" >10</td>
                              <td  class="redColor sz12" >16</td>
                              <td  class="redColor sz12" >27</td>
                              <td  class="blueColor sz12" >07</td>
                              <td   >460,145,954</td>
                              <td  class="redColor sz12" >19</td>
                              <td   >5,762,819</td>
                              <td   >225</td>
                              <td   >80,519</td>
                              <td   >785,993,086</td>
    
    # 获取当前多少期,根据'<td   >'切分为列表
    # 序号为1的是最新一期数据
    new_number = tr.split('<td   >')[1]
    print(new_number)   # 2020117</td>
    new_number = new_number.split('</td>')[0]
    print(new_number)   # 2020117
    print(new_number + '期开奖号码:', end='')  # 2020117期开奖号码:
    
    2020117</td>
    
    2020117
    2020117期开奖号码:
    
    # 根据'<td  class="redColor sz12" >'切分,获得红色球号码
    redtmp = tr.split('<td  class="redColor sz12" >')
    print(redtmp)
    
    ['"this.style.background=\'\'"\r\n\t\t                  onmouseover="this.style.background=\'#fff7d8\'">\r\n\t\t                  \r\n\t\t                  <td   >2020117</td>\r\n\t\t                  <td   >2020-11-22</td>\r\n\t\t                  ',
     '07</td>\r\n\t\t                  ',
     '08</td>\r\n\t\t                  ',
     '09</td>\r\n\t\t                  ',
     '10</td>\r\n\t\t                  ',
     '16</td>\r\n\t\t                  ',
     '27</td>\r\n\t\t                  <td  class="blueColor sz12" >07</td>\r\n\t\t                  <td   >460,145,954</td>\r\n\t\t                  ',
     '19</td>\r\n\t\t                  <td   >5,762,819</td>\r\n\t\t                  <td   >225</td>\r\n\t\t                  <td   >80,519</td>\r\n\t\t                  <td   >785,993,086</td>\r\n\t\t                  ']
    
    # 去掉第一个和最后一个没用的元素
    reds = redtmp[1:len(redtmp) - 1]  
    print(reds)
    
    ['07</td>\r\n\t\t                  ',
     '08</td>\r\n\t\t                  ',
     '09</td>\r\n\t\t                  ',
     '10</td>\r\n\t\t                  ',
     '16</td>\r\n\t\t                  ',
     '27</td>\r\n\t\t                  <td  class="blueColor sz12" >07</td>\r\n\t\t                  <td   >460,145,954</td>\r\n\t\t                  ']
    
    # 遍历reds列表,取出红色球号码,置于列表中
    red_number = [redstr.split('</td>')[0] for redstr in reds]
    print(*red_number)
    
    07 08 09 10 16 27
    
    # 获取蓝色球数字
    print('蓝球:', end='')
    blue = tr.split('<td  class="blueColor sz12" >')[1].split('</td>')[0]
    print(blue)
    
    蓝球:07
    

    完整代码如下:

    import urllib.request
    import urllib.parse
    import http.cookiejar
    
    
    def getHtml(url):
        cj = http.cookiejar.CookieJar()
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
        opener.addheaders = [('User-Agent',
                              'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'),
                             ('Cookie', '4564564564564564565646540')]
    
        urllib.request.install_opener(opener)
        html_bytes = urllib.request.urlopen(url).read()
        html_string = html_bytes.decode('utf-8')
        return html_string  # 返回网页的HTML内容
    
    
    # 最终输出结果格式如:2018134期开奖号码:03,16,18,31,32,33,蓝球:12
    html_content = getHtml("http://zst.aicai.com/ssq/openInfo/")  # 网页的HTML内容
    # print(html)
    
    # 开奖信息在表格中,获取表格中的数据
    table = html_content[html_content.find('<table class="fzTab nbt">'): html_content.find('</table>')]
    # print(table)
    
    # 根据'<tr \r\n\t\t                  onmouseout=' 将得到的字符串切分为列表,取序号为1的元素
    tmp = table.split('<tr \r\n\t\t                  onmouseout=')[1]
    # print(tmp)
    
    # 获取</tr>以前的第一行数据
    tr = tmp[: tmp.find('</tr>')]
    # print(tr)
    
    # 获取当前多少期,根据'<td   >'切分为列表
    # 序号为1的是最新一期数据
    new_number = tr.split('<td   >')[1]
    # print(new_number)   # 2020117</td>
    new_number = new_number.split('</td>')[0]
    # print(new_number)   # 2020117
    print(new_number + '期开奖号码:', end='')  # 2020117期开奖号码:
    
    # 根据'<td  class="redColor sz12" >'切分,获得红色球号码
    redtmp = tr.split('<td  class="redColor sz12" >')
    # print(redtmp)
    
    # 去掉第一个和最后一个没用的元素
    reds = redtmp[1:len(redtmp) - 1]
    # print(reds)
    
    # 遍历reds列表,取出红色球号码,置于列表中
    red_number = [redstr.split('</td>')[0] for redstr in reds]
    print(*red_number)
    
    # 获取蓝色球数字
    print('蓝球:', end='')
    blue = tr.split('<td  class="blueColor sz12" >')[1].split('</td>')[0]
    print(blue)
    

    输出:

    2020117期开奖号码:07 08 09 10 16 27
    蓝球:07
    2020134期开奖号码:02 09 10 20 22 26
    蓝球:01