Python中文
首页
教程
问答
标签
搜索
登录
注册
从html检索文本不适用于python
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在努力搜集公司的联系信息,除了电话号码以外,我还能得到其他所有的信息。这是html</p> <p/><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div^{cl2}$ <pre class="snippet-code-html lang-html prettyprint-override"><code><ul> <li> <h3>Harrrrrell INC</h3> </li> <li>43 Airpark Ct</li> <li>Alabaster, MD 35107</li> <li><span style="font-weight: bold;">Phone</span>: 888-232-8358</li> <li><span style="font-weight: bold;">Corporate URL</span>: <a href="http://www.hhsales.com" rel="nofollow" target="new">www.h23hsales.com</a></li> <li><span style="font-weight: bold;">More Detail</span>:<br> <a href="https://www.collierreporting.com/company/harrell-and-hall-enterprises-inc-alabaster-al">Click for Full Harrell &amp; Hall Enterprises INC Dossier</a></li> </ul></code></pre> </div> </div> <p>这个python脚本适用于这个html中除电话号码之外的所有其他内容。你知道吗</p> <pre><code>for companyLIST in result[0:]: try: companyname = companyLIST.find('h3').contents[0] print("Company Name ",str(companyname) ) except Exception as e: print("errror",str(e)) try: companySt = companyLIST.find_all('li')[1].contents[0] print("Company St ",str(companySt) ) except Exception as e: print("errror",str(e)) try: companyCity = companyLIST.find_all('li')[2].contents[0] print("Company City ",str(companyCity) ) except Exception as e: print("errror",str(e)) try: companyPhone= companyLIST.find('li')[3].contents[0] print("Company Phone ",companyPhone ) except Exception as e: print("errror",str(e)) try: companyWeb = companyLIST.find('a')['href'] print("Company Web ",str(companyWeb) ) print(" " ) except Exception as e: print("errror",str(e)) </code></pre> <p>这是的示例输出</p> <p>公司名称Harrrrell公司</p> <p>公司St 43 Airpark Ct</p> <p>马里兰州阿拉巴斯特市公司,邮编:35107</p> <p>错误3</p> <p>公司网站<a href="https://www.collierreporting.com/company/harrell-and-hall-enterprises-inc-alabaster-al" rel="nofollow noreferrer">https://www.collierreporting.com/company/harrell-and-hall-enterprises-inc-alabaster-al</a></p> <blockquote> <p><strong>Traceback (most recent call last):</strong></p> <pre><code> File "sample.py", line 26, in <module> companyPhone = soup.find('li')[3].contents[0] File "...dist-packages/bs4/element.py", line 1011, in __getitem__ return self.attrs[key] KeyError: 3 </code></pre> </blockquote> <p>如何重写下面的代码来获得电话号码?你知道吗</p> <pre><code>companyPhone= companyLIST.find('li')[3].contents[0] print("Company Phone ",companyPhone ) </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我猜您正在使用beatifulsoup4库解析HTML。如果是,您可以从html获取电话号码,如下所示:</p> <pre><code>text = soup.find_all('li')[3].contents[1] phone_number = re.sub(": ", "", text) print(phone_number) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python中从数据帧列中删除分类值?
4 回答
如何在python中从数据帧列表中删除引号
1 回答
如何在python中从数据帧创建列表
9 回答
如何在Python中从数据帧创建嵌套的JSON
3 回答
如何在Python中从数据帧显示wordcloud
1 回答
如何在Python中从数据帧的时间戳中删除字符
4 回答
如何在Python中从数据帧绘制简单绘图?
5 回答
如何在python中从数据帧行提取具有特定长度的范围?
5 回答
如何在python中从数据帧设置dict中的值
2 回答
如何在Python中从数据库中获得一个结果
10 回答
如何在python中从数据框中绘制分类条形图
10 回答
如何在Python中从数据框中选择特定细节?
9 回答
如何在python中从数据集中删除unicode
6 回答
如何在python中从数据集中删除某些数值?
1 回答
如何在python中从数据集中选择行
9 回答
如何在Python中从数组中删除元素
5 回答
如何在python中从数组中删除单个倒逗号?
7 回答
如何在python中从数组中删除对象?
7 回答
如何在python中从数组中删除引号
7 回答
如何在python中从数组中删除所有最小值
7 回答