【发布时间】:2019-02-06 22:47:40
【问题描述】:
我刚刚开始涉足 Python,正如许多人所做的那样,我从一个网络抓取示例开始尝试该语言。 我见过很多使用 zip 和 map 来组合列表的示例,但是我在尝试打印该列表时遇到了问题。 再说一次,我是新人,所以请温柔。
代码从 2 种特定标签类型(帖子的日期和标题)中收集所有内容,并将它们作为 2 个列表返回。为此,我正在使用 BeautifulSoup 和请求。 我正在练习这个测试的网站是一个名为“Staxel”的小游戏的博客
我可以让我的代码在 for 循环中使用 [soup.find] 和 [print] 打印一个标签的完整列表,但是当我尝试添加要打印的第二个列表时,我只是得到一个没有错误。 有关如何正确打印 2 个列表的任何提示?
我正在寻找类似的输出
进入 2019-01-06 新年
进入 2018-11-30 1.3.52 的 Staxel 更新日志
# import libraries
import requests
import ssl
from bs4 import BeautifulSoup
# set the URL string
quote_page = 'https://blog.playstaxel.com'
# query the website and return the html to give us a 'page' variable
page = requests.get(quote_page)
# parse the html using beautiful soup and store in a variable ... 'soup'
soup = BeautifulSoup(page.content, 'lxml')
# Remove the 'div' of name and get it's value
title_box = soup.find_all('h1',attrs={'class':'entry-title'})
date_box = soup.find_all('span',attrs={'class':'entry-date published'})
titles = [title.text.strip() for title in title_box]
dates = [date.text.strip()for date in date_box]
date_list = zip(dates, titles)
for heading in date_list:
print ("Entry {}")
【问题讨论】:
标签: python python-3.x list beautifulsoup