python3爬取https网页_爬取https网站

python2.7import urllib2import sslweburl= "https://www.douban.com/"webheader={'Accept': 'text/html, application/xhtml+xml, */*',#'Accept-Encoding': 'gzip, deflate','Accept-Language': 'zh-CN','User-Agen...
摘要由CSDN通过智能技术生成

python2.7

import urllib2

import ssl

weburl= "https://www.douban.com/"webheader={'Accept': 'text/html, application/xhtml+xml, */*',

#'Accept-Encoding': 'gzip, deflate','Accept-Language': 'zh-CN','User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko','DNT': '1','Connection': 'Keep-Alive','Host': 'www.douban.com'}

context=ssl._create_unverified_context()

req= urllib2.Request(url=weburl, headers=webheader)

webPage= urllib2.urlopen(req, context=context)

data= webPage.read().decode('utf-8')

print data

print type(data)

print type(webPage)

print webPage.geturl()

print webPage.info()

print webPage.getcode()

python 3.6

import

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Python网页数据并解析数据的教程: 1. 确定目标网站和要的信息 首先,需要确定要网站和要提的信息。可以使用Python的requests库向网站发送HTTP请求获HTML源代码,并使用BeautifulSoup库解析HTML文档获目标数据。 例如,我们要CSDN博客的文章标题和链接,可以先打开CSDN博客主页,右键查看网页源代码,找到文章标题和链接所在的HTML标签。 2. 发送HTTP请求获HTML源代码 接下来,使用Python的requests库向网站发送HTTP请求,获HTML源代码。 ``` import requests url = 'https://blog.csdn.net/' response = requests.get(url) html = response.text ``` 3. 解析HTML文档获目标数据 使用BeautifulSoup库解析HTML文档,获目标数据。 ``` from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'html.parser') titles = soup.find_all('div', class_='title') for title in titles: link = title.find('a').get('href') title_text = title.find('a').text.strip() print(title_text, link) ``` 上述代码中,通过`find_all`方法找到所有class属性为"title"的div标签,然后在每个div标签中找到第一个a标签,获链接和标题文本。 4. 完整代码 ``` import requests from bs4 import BeautifulSoup url = 'https://blog.csdn.net/' response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') titles = soup.find_all('div', class_='title') for title in titles: link = title.find('a').get('href') title_text = title.find('a').text.strip() print(title_text, link) ``` 以上就是一个简单的Python网页数据并解析数据的教程。需要注意的是,在网站数据时要遵守网站虫协议,避免被网站封禁IP。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值