我正在工作搜索网页抓取程序,但是当将打印语句转换为返回语句时,我遇到了“ TypeError:不支持解码str”,即使此转换公式在非for循环中也有效在一个循环内。
我尝试删除了一个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
这里是我从中复制解决方案的公式:
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
是否有更好的方法可以遍历结果以匹配列表或词典中的职位和公司?
我应该为此使用zip而不是列表吗?
答案 0 :(得分:0)
由于我要传递两个参数,所以我仅将参数分成两行:
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