python3爬取html页面内容

使用python3爬取html页面内容使用到的模块有 urllib,BeautifulSoup
使用下面代码就可以爬取网站小说的内容。但网站出于反爬考虑,把小说内容段落顺序打乱。
然后通过前端页面按一定规则拼重新排序,至于排序规则没有去研究,本文只展示爬取过程。

安装BeautifulSoup模块

pip3 install beautifulsoup4
Collecting beautifulsoup4
  Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB)
    100% |████████████████████████████████| 92kB 199kB/s 
Installing collected packages: beautifulsoup4
Successfully installed beautifulsoup4-4.6.0

代码

#!/usr/bin/python
#coding: UTF-8
from urllib.request import Request,urlopen 
from urllib.error import URLError, HTTPError
from bs4 import BeautifulSoup

def getContent(url):
	req = Request(url)
	#增加header头信息
	req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36')
	try: 
		response = urlopen(req)
		buff = response.read()
		html = buff.decode("utf8")
		response.close()
	except HTTPError as e:
		print('The server couldn\'t ful ll the request.')
		print('Error code: ', e.code)
	except URLError as e:
		print('reason:%s' %  e.reason)
	return html

def saveContent(content, url):
	soup = BeautifulSoup(content,"html.parser")
	for link in soup.find("dl",{"id":"dir"}).find_all('a'):
		title = link.get_text()
		url = "http://www.99lib.net"+link.get("href")
		print("title:%s" % title)
		print("url:%s" % url)
		html = getContent(url)
		soup2 = BeautifulSoup(html,"html.parser")
		content = soup2.find("div",{"id":"content"}).get_text()
		print("content:%s" % content)

url = "http://www.99lib.net/book/915/"
content = getContent(url)
saveContent(content, url)


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值