python写一个简单爬虫程序

python写一个简单爬虫程序

先看输出结果:

D:\应用集合\python>1.py
小说名称:诡秘之主
小说名称:超神机械师
小说名称:九星毒奶
小说名称:第一序列
小说名称:明天下
小说名称:烂柯棋缘
小说名称:亏成首富从游戏开始
小说名称:我师兄实在太稳健了
小说名称:轮回乐园
小说名称:当医生开了外挂
小说名称:学霸的黑科技系统
小说名称:我真没想重生啊
小说名称:手术直播间
小说名称:圣墟
小说名称:沧元图
小说名称:黎明之剑
小说名称:庆余年
小说名称:伏天氏
小说名称:斗罗大陆IV终极斗罗
小说名称:小阁老

D:\应用集合\python>

在这里插入图片描述

1.py

#coding=utf-8
import requests
from bs4 import BeautifulSoup
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')
 
def getHtmlurl(url):         #获取网址
    try:
       r=requests.get(url)
       r.raise_for_status()
       r.encoding=r.apparent_encoding
       return r.text
    except:
        return ""
 
def getpic(html): #获取图片地址并下载
    soup =BeautifulSoup(html,'html.parser')
    all_tag=soup.find('ul',class_='all-img-list cf').find_all('h4')
    for tag in all_tag:
       src=tag.string
       src.decode('utf-8')
       print ("小说名称:" + src)
def main():
    url='https://www.qidian.com/all'
    html=(getHtmlurl(url))
    getpic(html)
main()

Python如何安装模块Beautifulsoup4的模块
未安装会报错:ImportError: No module named bs4错误
下载BS4模块:
http://www.crummy.com/software/BeautifulSoup/bs4/download/4.3/beautifulsoup4-4.3.2.tar.gz

.解压到Python安装目录下的根目录中
C:\Python27\beautifulsoup4-4.3.2
python要在全局path中C:\Python27\ArcGISx6410.4

执行python setup.py install命令来进行安装

C:\Python27\beautifulsoup4-4.3.2>python setup.py install

```bash
C:\Python27\beautifulsoup4-4.3.2>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\bs4
copying bs4\dammit.py -> build\lib\bs4
copying bs4\diagnose.py -> build\lib\bs4
copying bs4\element.py -> build\lib\bs4
copying bs4\testing.py -> build\lib\bs4
copying bs4\__init__.py -> build\lib\bs4
creating build\lib\bs4\builder
copying bs4\builder\_html5lib.py -> build\lib\bs4\builder
copying bs4\builder\_htmlparser.py -> build\lib\bs4\builder
copying bs4\builder\_lxml.py -> build\lib\bs4\builder
copying bs4\builder\__init__.py -> build\lib\bs4\builder
creating build\lib\bs4\tests
......

----the—end—

参拷地址:https://blog.csdn.net/Jerry_liu20080504/article/details/86703662
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值