【问题标题】:Datatype mismatch beautifulsoup TypeError: unhashable type: 'list'数据类型不匹配 beautifulsoup TypeError: unhashable type: 'list'
【发布时间】:2017-01-26 21:42:03
【问题描述】:

我有一段代码访问links 并尝试在每个link 中找到某些keywords

最后,如果link一个或多个 keywords,它会将其存储在list 中。

但是,当我运行我的代码时,它给了我一个问题: TypeError: unhashable type: 'list' 在这一行:

for a in soup.find_all('a', class_="result-title hdrlnk", text=re.compile(job_kw,re.IGNORECASE)):

代码如下:

jobs_by_city = [
'http://boston.website.org/search/widget',
]

job_kw = [['web site','user', 'account'],['permission', 'name']]
job_kw = sum(job_kw, [])

jobs = []

for job_in_city in jobs_by_city:
    a_job = requests.get(job_in_city)
    soup = BeautifulSoup(a_job.text, "lxml")
    for a in soup.find_all('a', class_="result-title hdrlnk", text=re.compile(job_kw,re.IGNORECASE)):
        print(a.get('href'))
        #jobs.append(a.get('href'))

我在这里做错了什么?

【问题讨论】:

  • 你用的是哪个“美汤”版本? re.compile 不采用列表作为模式。我认为您可以将列表作为text 参数传递。在 BS v4 中,您可以将列表传递给 string 参数。

标签: python python-3.x parsing beautifulsoup


【解决方案1】:

re.compile 不接受 list 作为输入。您必须遍历关键字:

from bs4 import BeautifulSoup
import requests
import re

jobs_by_city = [
'http://boston.website.org/search/widget',
]

job_kws = [['web site','user', 'account'],['permission', 'name']]
job_kws = sum(job_kws, [])

jobs = []

for job_in_city in jobs_by_city:
    a_job = requests.get(job_in_city)
    soup = BeautifulSoup(a_job.text, "lxml")
    for job_kw in job_kws:
        for a in soup.find_all('a', class_="result-title hdrlnk", text=re.compile(job_kw,re.IGNORECASE)):
            print(a.get('href'))
            #jobs.append(a.get('href'))

给定的 url 没有提供您正在寻找的 html 元素 :)

【讨论】:

    猜你喜欢
    • 2015-02-11
    • 2019-10-11
    • 2020-03-27
    • 2020-05-11
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 2018-06-04
    • 2012-11-20
    相关资源
    最近更新 更多