【问题标题】:Issues using Regex on href with a tag using BeautifulSoup在 href 上使用正则表达式和使用 BeautifulSoup 的标签时出现问题
【发布时间】:2016-11-22 23:54:04
【问题描述】:

尝试从包含特定字符串的href标签中提取文本,下面是我的示例代码的一部分:

Experience = soup.find_all(id='background-experience-container')

Exp = {}

for element in Experience:
    Exp['Experience'] = {}


for element in Experience:
    role = element.find(href=re.compile("title").get_text()
    Exp['Experience']["Role"] = role


for element in Experience:
    company = element.find(href=re.compile("exp-company-name").get_text()
    Exp['Experience']['Company'] = company

它不喜欢我定义 Exp['outer_key']['inner_key'] = value 的语法,它返回 SyntaxError

我正在尝试建立一个 Dict.dict,其中包含有关角色和公司的信息,还希望包含每个人的日期,但还没有那么远。

谁能在我的代码中发现任何明显的错误?

非常感谢您对此的任何帮助!

【问题讨论】:

  • 似乎Exp['Experience']["Role"] = role 不起作用,因为它本质上是未初始化的。从另一张票看来,您似乎可以改用.append(...) 或预先初始化数组。
  • @Lupinity mine 是一个稍微不同的问题 - 我想构建如下输出:{Experience : {role: role_name, company: company_name}, {role: role_name, company: company_name}, ....}

标签: regex python-3.x web-scraping beautifulsoup


【解决方案1】:

find_all 可以返回许多值(即使您通过id 搜索)所以最好使用list 来保留所有值 - Exp = []

Experience = soup.find_all(id='background-experience-container')

# create empty list
Exp = []

for element in Experience:
    # create empty dictionary
    dic = {}

    # add elements to dictionary
    dic['Role'] = element.find(href=re.compile("title")).get_text()
    dic['Company'] = element.find(href=re.compile("exp-company-name")).get_text()

    # add dictionary to list
    Exp.append(dic)

# display

print(Exp[0]['Role'])
print(Exp[0]['Company'])

print(Exp[1]['Role'])
print(Exp[1]['Company'])

# or

for x in Exp:
    print(x['Role'])
    print(x['Company'])

如果您确定 find_all 只给您一个元素(并且您需要密钥 'Experience'),那么您可以这样做

Experience = soup.find_all(id='background-experience-container')

# create main dictionary
Exp = {}

for element in Experience:
    # create empty dictionary
    dic = {}

    # add elements to dictionary
    dic['Role'] = element.find(href=re.compile("title")).get_text()
    dic['Company'] = element.find(href=re.compile("exp-company-name")).get_text()

    # add dictionary to main dictionary
    Exp['Experience'] = dic

# display

print(Exp['Experience']['Role'])
print(Exp['Experience']['Company'])

Experience = soup.find_all(id='background-experience-container')

# create main dictionary
Exp = {}

for element in Experience:
    Exp['Experience'] = {
       'Role': element.find(href=re.compile("title")).get_text()
       'Company': element.find(href=re.compile("exp-company-name")).get_text()
    }

# display

print(Exp['Experience']['Role'])
print(Exp['Experience']['Company'])

【讨论】:

  • 感谢您的回复,我尝试使用您的第一个解决方案修改我的代码,但是我收到错误 dic['Company'] = element.find(href=re.compile("exp -company name").get_text() ^ SyntaxError: invalid syntax
  • 我在.get_text() 之前忘记了) - 即。 element.find(href=re.compile("exp-company-name")).get_text()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 2016-01-19
  • 2013-04-01
  • 2019-08-04
相关资源
最近更新 更多