为什么列表在Python中会给我一个语法错误?
这是我试图运行的代码:为什么列表在Python中会给我一个语法错误?,python,Python,这是我试图运行的代码: url = "https://remoteok.io/" response = requests.get(url,timeout=5) content= BeautifulSoup(response.content, "lxml") jobArr = [] for post in content.findAll('table', attrs={"id":"jobboard"}): postObject={ "company": post.find('td'
url = "https://remoteok.io/"
response = requests.get(url,timeout=5)
content= BeautifulSoup(response.content, "lxml") jobArr = []
for post in content.findAll('table', attrs={"id":"jobboard"}):
postObject={
"company": post.find('td', attrs={"class": "company position company_and_position"}).text.encode('utf-8'),
"job name": post.find('h3', attrs={"itemprop": "name"}).text.encode(utf-8),
"title": post.find('h2', attrs={"itemprop": "title"}).text.encode(utf-8),
"tags": post.find('td', attrs={"class": "tags"}).text.encode(utf-8),
"time": post.find('td', attrs={"class": "time"}).text.encode(utf-8),
"description": post.find('div', attrs={"class": "description"}).text.encode(utf-8),
"markdown": post.find("div", attrs={"class": "markdown"}).text.encode(utf-8)
}
print postObject
但每次我尝试运行该文件时,都会出现以下错误:
File "/home/user/Desktop/pythonscrap/webscraper.py", line 6
content= BeautifulSoup(response.content, "lxml") jobArr = [];
^
SyntaxError: invalid syntax
[Finished in 0.044s]
我不明白我错过了什么-请帮忙 这一行:
content= BeautifulSoup(response.content, "lxml") jobArr = []
没有任何意义。它有两个语句,所以它们需要在两行上:
content= BeautifulSoup(response.content, "lxml")
jobArr = []
您也可以使用分号来分隔这两条语句,但这在主观上是非常糟糕的风格,通常是不受欢迎的。您缺少一行新行,并且似乎有一个不需要的分号(尽管这只出现在您提供的错误中,而不是代码示例中): 实际上应该是:
content = BeautifulSoup(response.content, "lxml")
jobArr = []
只是一个余数。开始使用
Python3.x
,因为对Python2.x
的支持将在2019年底结束。但是,可以像content=BeautifulSoup(response.content,“lxml”)那样完成;jobArr=[]
。
content = BeautifulSoup(response.content, "lxml")
jobArr = []