我正在处理来自两个不同网页的数据集,但对于同一个人 - 数据集是合法信息.部分数据在第一页上可用,因此我使用正确的信息初始化Defendant对象,并设置我当前没有数据的属性null.这是班级:
class Defendant(object):
"""holds data for each individual defendant"""
def __init__(self,full_name,first_name,last_name,type_of_appeal,county,case_number,date_of_filing,
race,sex,dc_number,hair_color,eye_color,height,weight,birth_date,initial_receipt_date,current_facility,current_custody,current_release_date,link_to_page):
self.full_name = full_name
self.first_name = first_name
self.last_name = last_name
self.type_of_appeal = type_of_appeal
self.county = county
self.case_number = case_number
self.date_of_filing = date_of_filing
self.race = 'null'
self.sex = 'null'
self.dc_number = 'null'
self.hair_color = 'null'
self.eye_color = 'null'
self.height = 'null'
self.weight = 'null'
self.birth_date = 'null'
self.initial_receipt_date = 'null'
self.current_facility = 'null'
self.current_custody = 'null'
self.current_release_date = 'null'
self.link_to_page = link_to_page
Run Code Online (Sandbox Code Playgroud)
当我将一个半满的被告对象添加到被告列表时,这就是它的样子:
list_of_defendants.append(Defendant(name_final,'null','null',type_of_appeal_final,county_parsed_final,case_number,date_of_filing,'null','null','null','null','null','null','null','null','null','null','null','null',link_to_page))
Run Code Online (Sandbox Code Playgroud)
然后,当我从另一个页面获取其余数据时,我将这些属性设置为null,如下所示:
for …Run Code Online (Sandbox Code Playgroud) 我正在尝试安装Python包"distribute".我已将其下载并开始工作,但随后退出并显示此处出现的错误:
我有一种感觉,解决方案在某种程度上与我进入和定义sys_platform相关,但我没有足够的把握知道什么是错误的知道要解决什么.谢谢你的帮助!我总是对你们所有人的帮助感到震惊.
我正在学习“使用 Python 进行网页抓取”,而我则参与其中使用 MySQL 的部分。在 Google 上找不到任何对此错误消息特别有用的内容 - 你们中有人能帮我解码它吗?(希望能弄清楚如何修复它?!)输入命令后出现错误:
ALTER DATABASE scraping CHARACTER set = utf8mb4 COLLATE = utf8mb4_unicode_ci;
Run Code Online (Sandbox Code Playgroud)
输出:
ERROR 1 (HY000): Can't create/write to file './scraping/db.opt' (Errcode: 2)
mysql>
Run Code Online (Sandbox Code Playgroud) 试图从nyc wiki页面的高中名单中获取所有高中名称.
我已经写了足够多的脚本来获取包含高中,学术领域和入学标准的表格标签中包含的所有信息<tr>- 但我怎样才能将其缩小到我认为可以保留的范围内td[0](吐了回来a KeyError) - 只是学校的名字?
我到目前为止编写的代码:
from bs4 import BeautifulSoup
from urllib2 import urlopen
NYC = 'https://en.wikipedia.org/wiki/List_of_high_schools_in_New_York_City'
html = urlopen(NYC)
soup = BeautifulSoup(html.read(), 'lxml')
schooltable = soup.find('table')
for td in schooltable:
print(td)
Run Code Online (Sandbox Code Playgroud)
我收到的输出:
<tr>
<td><a href="/wiki/The_Beacon_School" title="The Beacon School">The Beacon School</a></td>
<td>Humanities & interdisciplinary</td>
<td>Academic record, interview</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
输出我正在寻求:
The Beacon School
Run Code Online (Sandbox Code Playgroud) 尝试编写一些代码,首先将玩家的姓名与他的工资请求相匹配。我能够编写它,以便通过从“sortcell”类中调用它来获取给定团队中每个球员的名字,但我似乎不知道如何获得薪水,因为他们都被称为 .
from bs4 import BeautifulSoup
from urllib import urlopen
teams = ['http://espn.go.com/nba/team/roster/_/name/atl/atlanta-hawks']
for team in teams:
html = urlopen('' + team)
soup = BeautifulSoup(html.read(), 'lxml')
names = soup.findAll("td", {"class": "sortcell"})
salary = soup.findAll("td", {"class": "td"})
print(salary)
for i in range(1, 15):
name = names[i].get_text()
print(name)
Run Code Online (Sandbox Code Playgroud)
您可以在以“薪水”开头的代码中看到我的(失败的)尝试。关于如何只获得薪资级别有什么想法吗?谢谢!
预期行为:
Salary 变量应该返回给定球员的工资,但目前不返回任何内容。