Discuss / Python / 哈喽 我又把老师的方法放弃了 人生苦短~

哈喽 我又把老师的方法放弃了 人生苦短~

Topic source

#首先复制源代码到本地文件test.html.

#下面是代码部分:

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

from bs4 import BeautifulSoup

fp = open('/Users/test/Downloads/study/test.html')
soup = BeautifulSoup(fp, 'lxml', from_encoding='utf-8')

print(soup.section.text)

#下面是输出结果: from the Python Events Calendar

Upcoming Events More

PyOhio 2017

29 July – 31 July 2017 Columbus, Ohio, USA

PyCon AU 2017

03 Aug. – 09 Aug. 2017 Melbourne Convention and Exhibition Centre, 1 Convention Centre Pl, South Wharf VIC 3006, Australia

PyCon KR 2017

12 Aug. – 16 Aug. 2017 COEX 513, Yeongdong-daero, Gangnam-gu Seoul 06164, Republic of Korea

PyCon Amazônia 2017

12 Aug. – 14 Aug. 2017 Manaus, Amazonas, Brazil

DjangoCon US 2017

13 Aug. – 19 Aug. 2017 Spokane, WA, USA

PyCon PL 2017

17 Aug. – 21 Aug. 2017 Hotel Ossa Congress & SPA, Ossa, Poland

You just missed...

SciPy 2017

10 July – 15 July 2017 Austin, TX, USA

EuroPython 2017

09 July – 17 July 2017 Rimini, Italy

#输出结果是字符串,所以可以随意编辑。OVER。

李昕越0o0

#2 Created at ... [Delete] [Delete and Lock User]

厉害了,大佬,解释下可好?

学习一下 bs4就可以了。

用htmlparser是为了练习,直接用库谁不会?你这不是聪明,是舍本逐末。要你用parser写你会写么?

F1renze丶

#5 Created at ... [Delete] [Delete and Lock User]

我反而觉得这样更好,廖大这方法挺麻烦的,楼上火气挺大啊,别忘了Python之禅

人生苦短,节约点时间吧。。。。 比比谁代码最少。哦哈哈。。。

Traceback (most recent call last): File "test.py", line 529, in <module> print(soup.section.text) AttributeError: 'NoneType' object has no attribute 'text'


  • 1

Reply