英文:
XPATH selection in Selenium Webdriver in Python
问题
以下是您提供的代码的翻译部分:
我卡在这里,正在尝试从 https://internet.safaricom.co.ke/faqs/home 上爬取一些数据。我希望使用Python爬虫来获取问题和答案,并将其存储在Pandas中。我遵循了这个代码:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://internet.safaricom.co.ke/5g-wireless/coverage'
driver = webdriver.Chrome()
driver.get(url)
# 等待表格加载完成
wait = WebDriverWait(driver, 10)
table = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="5G-coverage"]')))
soup = BeautifulSoup(table.get_attribute('outerHTML'), 'html.parser')
# 使用Pandas提取表格数据
dfs = pd.read_html(str(soup))
df = dfs[0]
这段代码似乎能够成功提取表格的详细信息。如何正确设置XPATH以处理 https://internet.safaricom.co.ke/faqs/home 页面上的可折叠部分?输出只需是一个Pandas数据框,其中一列是问题,另一列是每个FAQ的答案。提前感谢您的帮助。
<details>
<summary>英文:</summary>
I'm stuck looking at scraping some data here https://internet.safaricom.co.ke/faqs/home. I'm keen on getting the questions and answers in Pandas, thus a Python scraper is my preference. I followed this code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://internet.safaricom.co.ke/5g-wireless/coverage'
driver = webdriver.Chrome()
driver.get(url)
Wait for the table to be loaded
wait = WebDriverWait(driver, 10)
table = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="5G-coverage"]')))
soup = BeautifulSoup(table.get_attribute('outerHTML'), 'html.parser')
#Extract the table data using Pandas
dfs = pd.read_html(str(soup))
df = dfs[0]
The code seems to work well in extracting the table details. How can I have the XPATH right for this page https://internet.safaricom.co.ke/faqs/home that takes care of the collapsible parts? Output is just a Pandas df with one column with question and the other with Answer for each of the FAQs. Thanks in advance.
</details>
# 答案1
**得分**: 1
以下是带有解释的工作代码的翻译部分:
```python
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.maximize_window()
URL = "https://internet.safaricom.co.ke/faqs/home"
driver.get(URL)
wait = WebDriverWait(driver, 30)
time.sleep(3)
# 存储下拉菜单父级<div>元素的定位器
collapsible_parts = wait.until(EC.visibility_of_element_located((By.ID, 'simple-tabpanel-0')))
# 解析为BeautifulSoup对象
soup = BeautifulSoup(collapsible_parts.get_attribute('outerHTML'), 'html.parser')
# 打印文本内容
print(soup.text)
以下是控制台结果的翻译部分:
Safaricom Home “HOME PLUS”计划是什么?
这些是与您喜欢的HOME光纤计划一起提供的综合语音分钟、移动数据和短信捆绑套餐,让您可以选择满足您和您的亲人需求的捆绑套餐。这些捆绑套餐还附带2GB免费的YouTube捆绑套餐,以及在数据捆绑套餐完全使用并在有效期内后免费访问WhatsApp,以便您可以继续保持联系。
有哪些“Home Plus”捆绑套餐可用?
套餐价格
捆绑套餐
HOME PLUS 800
800肯尼亚先令
8GB + 400分钟 + 1000条短信
HOME PLUS 1800
1,800肯尼亚先令
17GB + 1000分钟 + 2000条短信
HOME PLUS 2700
2,700肯尼亚先令
27GB + 1500分钟 + 3000条短信
HOME PLUS 4600
4,600肯尼亚先令
47GB + 2500分钟 + 5000条短信
所有捆绑套餐有效期为30天
所有捆绑套餐都附带2GB免费的YouTube捆绑套餐和免费的WhatsApp访问,一旦数据捆绑套餐充分使用并在有效期内
客户将能够与其家庭成员共享捆绑套餐资源
如何购买“Home Plus”捆绑套餐?
拨打*400#,选择“获取Home Plus”,然后选择您喜欢的捆绑套餐。或者,在MySafaricom应用程序中,转到家庭互联网,选择Safaricom家庭光纤,然后选择“共享Home Plus”以购买Home Plus计划。
这些捆绑套餐会自动续订吗?
是的,这些捆绑套餐将在30天有效期后自动续订。
我是否可以与其他手机号码共享分钟、数据和短信资源?
是的,拨打*400#,选择“共享Home Plus”,或访问MySafaricom应用程序,选择Safaricom家庭光纤,家庭互联网,然后单击“共享Home Plus”以输入您所需受益人的手机号码。
“Home Plus”资源是否可以延期使用?
不,Home Plus资源将不会延期使用。
“Home Plus”资源是否会过期?
是的,未使用的资源将在第30天的23:59时过期。
如何添加或删除受益人?
拨打*400#,选择“共享Home Plus”,或在MySafaricom应用程序中,转到Safaricom家庭光纤,然后家庭互联网。单击“共享Home Plus”以更改您的受益人。
我可以添加多少个受益人?
您最多可以添加5个受益人以访问共享资源。
我可以在同一个月内购买不同金额的“Home Plus”捆绑套餐吗?
是的,您可以在同一个月内购买不同的捆绑套餐。您最后选择的捆绑套餐将在30天后自动续订。
我可以为其他号码购买“Home Plus”捆绑套餐吗?
您只能将其他号码添加为受益人。您将无法为其他号码购买Home Plus。
如果我错误购买了捆绑套餐,我该怎么办?
请联系400号客服以获得帮助并进行逆转。
有哪些可用的付款渠道?
1. USSD:拨打*400#,选择“Home Plus”,然后在选择您喜欢的捆绑套餐后,按照提示进行付款
2. MySafaricom应用程序:从Playstore或AppStore下载应用程序,然后在家庭互联网下选择Safaricom家庭光纤选项
如果我需要更多信息或帮助,可以使用哪些渠道?
如需帮助,请致电400号,该号码是您用于付费的号码。或者,您可以发送电子邮件至safaricomhome@safaricom.co.ke。您也可以通过社交媒体与我们联系:
1. Twitter:@Safaricom_Care
2. Facebook:Safaricom PLC
3. Zuri:WhatsApp,或将手机号码0722 000100保存到您的联系人中,然后打开聊天并键入“Hi Zuri”
进程以退出代码0完成
这是你要的翻译部分,不包
英文:
Refer the below working code with explanation:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.maximize_window()
URL = "https://internet.safaricom.co.ke/faqs/home"
driver.get(URL)
wait = WebDriverWait(driver, 30)
time.sleep(3)
# Store the locator of dropdown parent <div> element
collapsible_parts = wait.until(EC.visibility_of_element_located((By.ID, 'simple-tabpanel-0')))
# Parse it into soup
soup = BeautifulSoup(collapsible_parts.get_attribute('outerHTML'), 'html.parser')
# print the text
print(soup.text)
Console Result:
What are the Safaricom Home “HOME PLUS” Plans?These are integrated Voice Minutes, mobile Data and SMS bundles, availed alongside your preferred HOME Fibre plans, and that allow you to choose a bundle that meets you and your loved ones’ needs.The bundles also come with a FREE 2GB YouTube bundle and free access to WhatsApp after the data bundle is fully utilized and is within the validity period, so that you can continue keeping in touch.What “Home Plus” bundles are available?PackagePriceBundleHOME PLUS 800Ksh. 8008GB + 400Min + 1000SMSHOME PLUS 1800Ksh.1,80017GB + 1000Min + 2000SMSHOME PLUS 2700Ksh. 2,70027GB + 1500Min + 3000SMSHOME PLUS 4600Ksh.4,60047GB + 2500Min + 5000SMSAll bundles are valid for 30 days All bundles come with a 2GB Free YouTube bundle and Free WhatsApp access upon full utilization of the data bundle Customers will be able to share the bundle resources with members of their householdHow do I purchase the “Home Plus” bundles?Dial *400# select “Get Home Plus” and choose your preferred bundle. Alternatively, on MySafaricom App, go to Home Internet, select Safaricom Home Fiber, then 'Share Home Plus' to purchase a Home Plus plan.Will the bundles auto renew?Yes, the bundles will auto renew after the 30-day validity period.Will I be able to share the minutes, data and SMS resources with other mobile numbers?Yes, dial *400# and select “Share Home Plus” or visit MySafaricom app, Select Safaricom Home Fibre, Home internet and click 'Share Home Plus' to input the mobile numbers of your desired beneficiaries.Will the “Home Plus” resources rollover?No, Home Plus resources will not rollover.Do the “Home Plus” resources expire?Yes, Unutilized resources will expire on 30th day past 23:59 hours.How do I add or delete a beneficiary?Dial *400# and select “Share Home Plus” or on MySafaricom app, go to Safaricom Home Fiber then Home internet. Click on 'Share Home Plus' to change your beneficiaries.How many beneficiaries can I add?You can add up to 5 beneficiaries to access the shared resources.Can I purchase 2 different “Home Plus” bundles for different amounts within the same month?Yes. You can purchase different bundles within the month. The last one you will settle on, will be the one to be renewed once 30 days elapse.Can I purchase the “Home Plus” bundles for another number?You can only add another number as a beneficiary. You will not be able to purchase Home Plus for another number.What should I do if I purchase a bundle erroneously?Please contact the contact center on 400 for assistance and for the reversal to be affected.What are the available payment channels?1. USSD : dial *400# select “Home Plus” and after selecting your preferred bundle, follow the prompts to make payment 2. MySafaricom app : download from your Playstore or AppStore and select the Safaricom Home Fiber option under Home Intenet In case I need more information or assistance, which channels can I use?For assistance, please call 400 from the number that you used to pay for the service. Alternatively, you can send an email to safaricomhome@safaricom.co.ke. You can also reach us on social media: 1. Twitter: @Safaricom_Care2. Facebook: Safaricom PLC 3. Zuri: WhatsApp or save mobile number 0722 000100 on your contacts, open chat and type ‘Hi Zuri’
Process finished with exit code 0
答案2
得分: 1
以下是翻译好的部分代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://internet.safaricom.co.ke/faqs/home'
driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="panel1a-header"]')))
element.click()
expanded_element = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="panel1a-content"]/div/p')))
print(expanded_element.text)
结果:
这些是集成的语音分钟、移动数据和短信套餐,与您首选的HOME光纤套餐一同提供,让您选择符合您和您的亲人需求的套餐。
这些套餐还附带免费的2GB YouTube套餐以及数据套餐用尽后仍在有效期内提供免费访问WhatsApp的功能,以便您继续保持联系。
英文:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://internet.safaricom.co.ke/faqs/home'
driver = webdriver.Chrome()
driver.get(url)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="panel1a-header"]')))
element.click()
expanded_element = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="panel1a-content"]/div/p')))
print(expanded_element.text)
Result:
These are integrated Voice Minutes, mobile Data and SMS bundles, availed alongside your preferred HOME Fibre plans, and that allow you to choose a bundle that meets you and your loved ones’ needs.
The bundles also come with a FREE 2GB YouTube bundle and free access to WhatsApp after the data bundle is fully utilized and is within the validity period, so that you can continue keeping in touch.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论