python 提取页面标签 /源码-----简单代码

# ! /usr/bin/env python
#
 -*- coding: utf-8 -*-


import  urllib                                       
sock 
=  urllib.urlopen( " http://www.bitunion.org/ "
html  
=  sock.read()                            
sock.close()
html 
=  unicode(html  , " gbk "  )
print  html


# !/usr/bin/env python
from  sgmllib  import  SGMLParser

import  urllib                                    
sock 
=  urllib.urlopen( " http://www.bitunion.org/ "
html 
=  sock.read()
sock.close()
html 
=  unicode(html  , " gbk "  )
# print html 
=   html
class  Parse(SGMLParser):
    
def  reset(self):
        self.found_title 
=  0
        SGMLParser.reset(self)
    
def  start_title(self, attrs):
        self.found_title 
+=   1
    
def  end_title(self):
        self.found_title 
-=   1
    
def  handle_data(self, text):
        
if  self.found_title  >  0:
            
print   ' Title: %s '   %  text

=  Parse()
p.feed(s) 
 
 


最后注视
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 为了爬取 https://www.ccdi.gov.cn/scdcn/ 的内容,我们可以使用 Python 的第三方库 requests 和 BeautifulSoup。 首先,需要安装 requests 和 BeautifulSoup,可以通过 pip 安装: ``` pip install requests pip install beautifulsoup4 ``` 然后,可以使用下面的代码爬取 https://www.ccdi.gov.cn/scdcn/ 的内容: ```python import requests from bs4 import BeautifulSoup url = 'https://www.ccdi.gov.cn/scdcn/' response = requests.get(url) html_content = response.text soup = BeautifulSoup(html_content, 'html.parser') print(soup.prettify()) ``` 这段代码将会打印出爬取到的网页的源代码。您可以使用 BeautifulSoup 的各种方法来解析和提取网页的信息。 注意: 此网站有反爬虫措施,在爬取之前请先阅读相关网站的爬虫政策。 ### 回答2: 爬取网页内容可以使用Python的requests库和BeautifulSoup库。首先,我们要使用requests库向指定的网址发送请求获取网页的内容。然后,使用BeautifulSoup库解析网页HTML代码,提取出需要的内容。 下面是示例代码: ```python import requests from bs4 import BeautifulSoup url = 'https://www.ccdi.gov.cn/scdcn/' # 发送请求获取网页内容 response = requests.get(url) content = response.content # 解析网页内容 soup = BeautifulSoup(content, 'html.parser') # 提取需要的内容 title = soup.title.text body = soup.find('div', class_='body-content').text # 打印提取的内容 print('网页标题:', title) print('网页内容:', body) ``` 以上代码,我们通过requests库的`get`方法向指定的网址发送了一个GET请求,并将得到的响应内容保存在`response`变量。然后,使用BeautifulSoup库对响应内容进行解析,设置解析器为`html.parser`。通过查看网页HTML源码,我们确定要提取的内容在`<div class="body-content">`标签,因此使用了`soup.find('div', class_='body-content')`方法提取出该标签的内容。最后,我们将提取的标题和内容分别保存在`title`和`body`变量,并打印出来。 使用这段代码,我们可以从https://www.ccdi.gov.cn/scdcn/网页爬取到网页的标题和内容。注意,爬取网页内容时要遵守网站的爬虫协议和法律法规,不要对网站造成过大的负载。 ### 回答3: 要编写Python代码爬取https://www.ccdi.gov.cn/scdcn/的内容,我们可以使用第三方库requests和BeautifulSoup来实现。以下是一种可能的实现方式: ```python import requests from bs4 import BeautifulSoup # 发起HTTP请求获取页面内容 url = "https://www.ccdi.gov.cn/scdcn/" response = requests.get(url) html_content = response.text # 使用BeautifulSoup解析页面内容 soup = BeautifulSoup(html_content, "html.parser") # 找到需要爬取的内容所在的HTML元素 content_div = soup.find("div", class_="content_list") items = content_div.find_all("li") # 遍历爬取到的内容并提取信息 for item in items: title = item.find("a").text href = item.find("a")["href"] date = item.find("span").text # 打印提取到的信息 print("标题:", title) print("链接:", href) print("日期:", date) print("-----------------") ``` 上述代码使用requests库发送HTTP GET请求获取页面内容,然后使用BeautifulSoup库解析HTML页面。我们找到页面的`content_list`类的`div`元素,并在其找到所有的`li`元素。接着,我们可以遍历这些`li`元素,从提取标题、链接和日期等信息,并将其打印出来。 需要注意的是,爬取网页内容时,可能需要处理反爬机制、登录或者cookie等问题。具体的实现可能因目标网站的特殊要求而有所不同,因此可根据实际情况做相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值