简体   繁体   English

在for循环内的for循环中附加str时,出现“ TypeError:不支持解码str”

[英]'TypeError: decoding str is not supported' when appending str in for loop within a for loop

I'm working on a job search web scraper but when converting a print statement into a return statement, I run into a 'TypeError: decoding str is not supported' even though this convertion formula works on a non-for-loop-within-a-for-loop. 我正在研究求职网络抓取工具,但是当将打印语句转换为return语句时,我遇到了“ TypeError:不支持解码str”,即使此转换公式适用于非for-for-in-loop-一个循环。

I tried removing one of the str variables and this worked but what I need is a duple list including company and jobs 我试着删除了一个str变量,但是这个可行,但是我需要一个包含公司和职位的双重清单

def get_company_and_jobs():
    """this function scrapes the company names 
    and job titles"""
    comps_and_jobs = []
    companyName = pageSoup.find_all('span', class_='company')
    jobTitle = pageSoup.find_all('div', class_='title')
    for span in jobTitle:
        for x in companyName:
            comps_and_jobs.append(str(x.text,span.text))
            # # This is before I added a list
            # print(x.text,span.text)
    return comps_and_jobs
TypeError                                 Traceback (most recent call last)
<ipython-input-60-9bcc02c8c200> in <module>
      4 for span in jobTitle:
      5     for x in companyName:
----> 6             comps_and_jobs.append(str(x.text,span.text))
      7             # # This is before I added a list
      8             # print(x.text,span.text)

TypeError: decoding str is not supported

Here is the same formula that I was copying the solution from: 这是我从中复制解决方案的公式:

def get_company_names():
    """this function scrapes the company names"""
    comp_names = []
    companyName = pageSoup.find_all('span', class_='company')
    for span in companyName:
        comps_names.append(str(span.text))
    ## This is before I added a list
    #     print(span.text)
    return comp_names

Is there a better way I could iterate over results to match both job and company in a list or dictionary? 有没有更好的方法可以遍历结果以匹配列表或词典中的职位和公司?

Should I be using zip for this instead of a list? 我应该为此使用zip而不是列表吗?

Being that I was passing two arguments, I simply split the arguments into two lines: 由于我要传递两个参数,因此我将参数分为两行:

def get_company_and_jobs():
    """this function scrapes the company names 
    and job titles"""
    comps_and_jobs = []
    companyName = pageSoup.find_all('span', class_='company')
    jobTitle = pageSoup.find_all('div', class_='title')
    for span in jobTitle:
        for x in companyName:
            comps_and_jobs.append(str(x.text))
            comps_and_jobs.append(str(span.text))
    return comps_and_jobs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM