找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2718|回复: 5

[代码与实例] 爬虫

5

主题

15

帖子

15

积分

贫民

积分
15
wx_Z9LTRnIn 发表于 2021-6-5 18:02:38 | 显示全部楼层 |阅读模式
可不可以麻烦大佬发一份成功爬取网上某篇文章的某一段落的文字,就是
http://www.ruiwen.com/wenxue/zhuziqing/419754.html
朱自清散文,只爬取开头一段
回复

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
我爱喝奶茶 发表于 2021-6-14 16:52:18 | 显示全部楼层
这几天心里颇不宁静。今晚在院子里坐着乘凉,忽然想起日日走过的荷塘,在这满月的光里,总该另有一番样子吧。月亮渐渐地升高了,墙外马路上孩子们的欢笑,已经听不见了;妻在屋里拍着闰儿,迷迷糊糊地哼着眠歌。我悄悄地披了大衫,带上门出去。
回复 支持 反对

使用道具 举报

0

主题

1

帖子

1

积分

贫民

积分
1
wanghan519 发表于 2021-6-16 09:28:19 | 显示全部楼层
curl "http://www.ruiwen.com/wenxue/zhuziqing/419754.html" -s | iconv -f gb18030 -t utf-8 | grep 'class="content' -A1
回复 支持 反对

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
noobyxg 发表于 2021-6-18 14:41:15 | 显示全部楼层
几天心里颇不宁静。今晚在院子里坐着乘凉,忽然想起日日走过的荷塘,在这满月的光里,总该另有一番样子吧。月亮渐渐地升高了,墙外马路上孩子们的欢笑,已经听不见了;妻在屋里拍着闰儿,迷迷糊糊地哼着眠歌。我悄悄地披了大衫,带上门出去。
回复 支持 反对

使用道具 举报

0

主题

17

帖子

17

积分

贫民

积分
17
一杆钓起满天星 发表于 2021-7-4 11:15:36 | 显示全部楼层
  1. import requests
  2. from lxml import etree
  3. url = 'http://www.ruiwen.com/wenxue/zhuziqing/419754.html'
  4. def getcontent(url):
  5.     headers = {
  6.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
  7.     }
  8.     resp**e = requests.get(url, headers=headers)
  9.     resp**e.encoding = "gbk"
  10.     html = etree.HTML(resp**e.text)
  11.     content = html.xpath('/html/body//div[@class="content"]/*/text()')
  12.     return content

  13. datalist = getcontent(url)
  14. for line in datalist:
  15.     print(line.strip('\u3000') + "\r")
复制代码
回复 支持 反对

使用道具 举报

0

主题

17

帖子

17

积分

贫民

积分
17
一杆钓起满天星 发表于 2021-7-4 11:21:09 | 显示全部楼层
import requests
from lxml import etree
url = 'http://www.ruiwen.com/wenxue/zhuziqing/419754.html'
def getcontent(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    }
    resp**e = requests.get(url, headers=headers)
    resp**e.encoding = "gbk"
    html = etree.HTML(resp**e.text)
    content = html.xpath('/html/body//div[@class="content"]/*/text()')
    return content

datalist = getcontent(url)
for line in datalist:
    print(line.strip('\u3000') + "\r")

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表