Discuss / Python / 蛤

看了楼下大神的,这是BeautifulSoup的教程http://cuiqingcai.com/1319.html

from urllib import request
from bs4 import BeautifulSoup

page = request.urlopen('https://www.python.org/events/python-events')
soup = BeautifulSoup(page,'lxml')

count=0
for i in soup.select('h3[class="event-title"]'):
   count=count+1

for i in range(count):
   print('event-title:',soup.select('h3[class="event-title"]')[i].get_text())
   print('event-time:',soup.select('time')[i].get_text())
   print('event-location:',soup.select('span[class="event-location"]')[i].get_text())
   print('-'*20)

event-title: PyCon Pune 2017 event-time: 16 Feb. – 20 Feb. 2017

event-location: COEP, Pune, India

event-title: Iran Pycon "IrPycon" event-time: 17 Feb. – 18 Feb. 2017

event-location: Elm va San'at University, Tehran, Iran

event-title: Swiss Python Summit event-time: 17 Feb. – 18 Feb. 2017

event-location: Rapperswil, Switzerland

event-title: PyWeek 23 - Python community game jam event-time: 19 Feb. – 27 Feb. 2017

event-location: Online Event

event-title: PyCon Philippines 2017 event-time: 25 Feb. – 27 Feb. 2017

event-location: Cagayan de Oro City, Philippines

event-title: PyCon SK 2017 event-time: 10 March – 13 March 2017

event-location: Bratislava, Slovakia

event-title: Rencontres Django 2017 event-time: 01 April – 03 April 2017

event-location: Toulon, France

event-title: DjangoCon Europe 2017 event-time: 03 April – 08 April 2017

event-location: Florence, Italy

event-title: PythonCamp 2017 event-time: 08 April – 10 April 2017

event-location: GFU Cyrus AG , Am Grauen Stein 27, 51105 Köln, Germany

event-title: PyDays Vienna event-time: 05 May – 07 May 2017

event-location: FH Technikum Wien, Höchstädtplatz 6, 1200 Vienna, Austria

event-title: GeoPython 2017 event-time: 08 May – 11 May 2017

event-location: Basel, Switzerland

event-title: PyCon US 2017 event-time: 17 May – 26 May 2017

event-location: Portland, Oregon, USA

event-title: EuroPython 2017 event-time: 09 July – 17 July 2017

event-location: Rimini, Italy

event-title: PyOhio 2017 event-time: 29 July – 31 July 2017

event-location: Columbus, Ohio, USA

event-title: PyCon AU event-time: 03 Aug. – 09 Aug. 2017

event-location: Melbourne Convention and Exhibition Centre, 1 Convention Centre Pl, South Wharf VIC 3006, Australia

event-title: PyCon Amazônia event-time: 12 Aug. – 14 Aug. 2017

event-location: Manaus, Amazonas, Brazil

event-title: DjangoCon US 2017 event-time: 13 Aug. – 19 Aug. 2017

event-location: Spokane, WA, USA

event-title: PyCon APAC 2017 event-time: 26 Aug. – 28 Aug. 2017

event-location: Kuala Lumpur, Malaysia

event-title: PyCon UK 2017 event-time: 26 Oct. – 31 Oct. 2017

event-location: Cardiff City Hall, Gorsedd Gardens Rd, Cardiff CF10 3ND, UK

[Finished in 2.1s]

count省掉,这样写简洁些吧

for i in range(len(soup.select('h3[class="event-title"]'))):
   ...

  • 1

Reply