python - BeautifulSoup 循环无法捕获内容
问题描述
我的 BeautifulSoup 刮板循环遍历 URL 列表并根据需要捕获我的标准之一('month_year')。但是第二个循环中的每个“li”标签仅提取页面上第一个实例的详细信息。
对于给定的 URL,'month_year' 不会改变,这很好。但是'broadcast' 应该为每个'li' 标签返回不同的细节。
对新手有什么建议吗?
import csv
from bs4 import BeautifulSoup
from urllib.request import urlopen
contents = []
with open('monthly_list.csv','r') as csvf:
urls = csv.reader(csvf)
for url in urls:
page = urlopen(url[0]).read()
tunes = BeautifulSoup(page, 'html.parser')
playlist = tunes.find('ul', class_='content-container row')
daily_URLs_data = open('daily_URLs_2007-2018.csv', 'a')
for li in playlist.find_all('div', class_='description'):
month_year = tunes.find('div', class_='resultsHeader').text
broadcast = playlist.h3.a
print (month_year)
print (broadcast)
csv_writer.writerow([month_year, broadcast])
解决方案
推荐阅读
- angular - Angular:如何动态更改 scss 变量?
- ios - 我正在尝试从 Swift subreddit 解析 JSON。为什么我有问题?
- xamarin - 有没有办法使用 MvvmCross 在 Xamarin Forms 中使用嵌套的 CollectionViews?
- c++ - 基于范围的循环,自动引用指针
- redis - 如何使用生菜反应api处理get命令的空响应
- flowtype - 流返回错误的类型
- java - 如何将其转换为 Mockito 2.2?
- c# - 无法从 MVC 发送邮件
- python - 如何在定义函数时消除python中的索引错误
- node.js - 如何在 AppEngine 上使用 Angular 提供静态文件