如何使用Python报废Selenium中的特定特征

时间:2019-11-18 11:33:41

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver

我想在此HTML代码中取消70个字符:

<p>2) Proof of payment emailed to satrader03<strong>@gmail.com</strong> direct from online banking 3) Selfie of you holding your ID 4) Selfie of you holding your bank card from which payment will be made OR 5) Skype or what's app Video call while logged onto online banking displaying account name which should match personal verified name Strictly no 3rd party payments</p>

我想知道如何用硒来废弃特定字符,例如,我想废弃30个字符或其他字符

这是我的代码:

description = driver.find_elements_by_css_selector("p")
items = len(title)
with open('btc_gmail.csv','a',encoding="utf-8") as s:
    for i in range(items):
        s.write(str(title[i].text) + ',' + link[i].text + ',' + description[i].text + '\n')

如何剪贴30个字符或70个字符


修改


完整代码:

driver = webdriver.Firefox()

r = randrange(3,7)


for url_p in url_pattren:   
    time.sleep(3)   
    url1 = 'https://www.bing.com/search?q=site%3alocalbitcoins.com+%27%40gmail.com%27&qs=n&sp=-1&pq=site%3alocalbitcoins+%27%40gmail.com%27&sc=1-31&sk=&cvid=9547A785CF084BAE94D3F00168283D1D&first=' + str(url_p) + '&FORM=PERE3'
    driver.get(url1)
    time.sleep(r)
    title = driver.find_elements_by_tag_name('h2')
    link = driver.find_elements_by_css_selector("cite")
    description = driver.find_elements_by_css_selector("p")
    items = len(title)
    with open('btc_gmail.csv','a',encoding="utf-8") as s:
        for i in range(items):
            s.write(str(title[i].text) + ',' + link[i].text + ',' + description[i].text[30:70] + '\n')

有解决方案吗?

谢谢!

1 个答案:

答案 0 :(得分:1)


您可以获取标签的文本,然后在字符串上使用切片

>>> description = driver.find_elements_by_css_selector("p")[0].text 
>>> print(description[30:70])  # printed from 30th to 70th symbol
'satrader03<strong>@gmail.com</strong>'