Python 我能';在美丽的群山中找不到桌子
所以我对这东西很陌生,所以这可能很愚蠢。但我似乎不明白为什么这一行非常基本的代码找不到任何表。。。还试图找到一个表来获得基本上每一行。 网址:Python 我能';在美丽的群山中找不到桌子,python,beautifulsoup,Python,Beautifulsoup,所以我对这东西很陌生,所以这可能很愚蠢。但我似乎不明白为什么这一行非常基本的代码找不到任何表。。。还试图找到一个表来获得基本上每一行。 网址: 从selenium导入webdriver 从selenium.webdriver.firefox.options导入选项 作为pd进口熊猫 选项=选项() options.add_参数('--headless') driver=webdriver.Firefox(options=options) 驱动程序。获取(“https://uwflow.com/c
从selenium导入webdriver
从selenium.webdriver.firefox.options导入选项
作为pd进口熊猫
选项=选项()
options.add_参数('--headless')
driver=webdriver.Firefox(options=options)
驱动程序。获取(“https://uwflow.com/course/cs136")
df=pd.read\u html(driver.page\u源)[0]
df.to_csv('out.csv',index=False)
driver.quit()
输出:
我选了《2020年冬季》,如果你想2020年春季
,那么你需要将[0]
更改为[1]
数据存储在
标签内的页面中。如果您想要不使用selenium
的解决方案,可以使用re
和json
模块解析数据
例如:
import re
import json
import requests
url = 'https://uwflow.com/course/cs136'
txt = requests.get(url).text
data = json.loads(re.findall(r'window.pageData.courseObj = (\{.*?});', txt)[0])
# print(json.dumps(data, indent=4)) # <-- uncomment this to see all data
print(data['code'] + ' - ' + data['name'])
print(data['description'])
print('{:<10} {:<10} {:<10}'.format('Class', 'Enrolled', 'Campus'))
for section in data['sections']:
print('{:<10} {:<10} {:<10}'.format(section['class_num'],
str(section['enrollment_total']) + '/' + str(section['enrollment_capacity']),
section['campus']))
Tbh我想知道为什么这不是一个用漂亮的汤制作的工作,而不是一个通过JavaScript呈现的替代页面,所以这就是为什么我使用Selenium谢谢!作为将来的参考,我如何知道它是否包含在脚本标记中?因为当我检查元素并看到html时,只有body、几个div和table@MostafizurRahman当我在Firefox中单击查看源代码并搜索字符串时,例如class
5914
,然后只匹配了
内的标签以及其他数据。
import re
import json
import requests
url = 'https://uwflow.com/course/cs136'
txt = requests.get(url).text
data = json.loads(re.findall(r'window.pageData.courseObj = (\{.*?});', txt)[0])
# print(json.dumps(data, indent=4)) # <-- uncomment this to see all data
print(data['code'] + ' - ' + data['name'])
print(data['description'])
print('{:<10} {:<10} {:<10}'.format('Class', 'Enrolled', 'Campus'))
for section in data['sections']:
print('{:<10} {:<10} {:<10}'.format(section['class_num'],
str(section['enrollment_total']) + '/' + str(section['enrollment_capacity']),
section['campus']))
CS 136 - Elementary Algorithm Design and Data Abstraction
This course builds on the techniques and patterns learned in CS 135 while making the transition to use an imperative language. It introduces the design and analysis of algorithms, the management of information, and the programming mechanisms and methodologies required in implementations. Topics discussed include iterative and recursive sorting algorithms; lists, stacks, queues, trees, and their application; abstract data types and their implementations.
Class Enrolled Campus
6214 90/90 UW U
6011 59/65 UW U
5914 46/90 UW U
6004 90/90 UW U
6048 90/90 UW U
6109 90/90 UW U
6215 87/90 UW U
6260 90/90 UW U
6261 67/90 UW U
6005 64/65 UW U
... and so on.