Python爬虫,BeautifulSoup爬取标签内容;同时:出现列表异常数据,删除其中一列
需要用到的库,prettytable 自动生成表单打印
import prettytable as pt
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 4 16:07:21 2020
@author: 权
"""
import prettytable as pt
import requests
from bs4 import BeautifulSoup
url ="http://58921.com/"
r=requests.get(url,timeout=10)
r.encoding='utf-8'
html=r.text
soup=BeautifulSoup(html,"html.parser")
movies = soup.find('tbody')
movies2=movies.find_all('tr')
del movies2[-1]
tb = pt.PrettyTable()
tb.field_names = ["电影名称", "当日排片"]
for i in movies2:
names=i('td')[0].text
values=i('td')[1].text
tb.add_row([names, values])
print(tb)