python - 我可以不将美丽汤中的 .text 附加到 python DataFrame
问题描述
我正在尝试遍历 URL 列表,以使用 requests.get() 和美丽的汤从网站中提取特定文本。URL 恢复正常,我还可以打印我正在寻找的文本(电话号码),它工作正常,但是TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid
当我尝试附加到以前用空 [] 创建的数据帧时收到一个。
try:
response = requests.get(f"https://www.yellowpages.com/search?search_terms={sc_df['Business Name'][i]}&geo_location_terms={sc_df['City'][i]}+{sc_df['State'][i]}")
soup = BeautifulSoup(response.text, 'html.parser')
test = soup.find('div', attrs = {'class':'info'}).find('div', attrs = {'class':'phones phone primary'})
if test is not None:
text = test.text
print(str(text))
new_number_df['Business Name'].append(sc_df['Business Name'][i])
new_number_df['Phone'].append(text)
else:
test = None
print(test)
except requests.exceptions.RequestException as e:
raise SystemExit(e)```
解决方案
正如你的错误所说,你
无法连接类型“<class 'str'> 的对象
所以您需要将您的添加转换为 pandas.Series 对象,然后附加它,如下所示:
new_number_df['Phone'].append(pandas.Series([text]))
希望这能解决您的问题!
推荐阅读
- angularjs - Angular 和 AngularJS 混合应用程序路由:Angular 组件作为子状态不呈现
- java - BufferedImage getSubimage 以看似不错的论点抛出异常
- .net - netstandard2.0 (.net standard 2.0) 类库不能引用 net461 (.net framework 4.6.1) 类库
- sql-server - SQL Server 中的 Nvarchar
- go - 处理两个连续错误
- java - 从文件中读取 - 堆满
- jdbc - 如何在 Telosys CLI 中设置 JDBC 驱动程序 JAR 以创建 dbmodel
- spring - 在 RuntimeEnvironment 中包含 BPMN 文件的问题
- oracle - Oracle Scheduler proc 未在下一次开始时间之前完成
- java - 将 Perl 正则表达式转换为 Java