python - 删除 "?"python 之后的所有内容

标签 python pandas selenium web-scraping

当我尝试去掉“?”后面的所有内容时出现错误在一组抓取的链接中:

代码:

from selenium import webdriver
import pandas as pd
import time 
from datetime import datetime
from collections import OrderedDict
import re

browser = webdriver.Firefox()
browser.get('https://www.kickstarter.com/discover?ref=nav')
categories = browser.find_elements_by_class_name('category-container')

category_links = []
for category_link in categories:
    category_links.append((str('https://www.kickstarter.com'),
                     category_link.find_element_by_class_name('bg-white').get_attribute('href')))
    print(category_links)
    for i in category_link:
    category_links2 = re.sub('?$', '', category_links)
    print(category_links2)

错误:

TypeError: 'FirefoxWebElement' object is not iterable

最佳答案

  1. 您需要遍历 category_linksi 是循环变量

  2. 您需要在 i 上应用 re.sub,而不是 category_links,因为后者是一个列表,而 re.sub 不适用于列表。

  3. 对于像这样的简单任务,我建议使用 str.split? 上拆分:


for i in category_links:
    category_links2 = i[1].split('?')[-1]
    print(category_links2)

关于python - 删除 "?"python 之后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45306676/

相关文章:

python - markdown2 模块 - 如何从 Python 脚本中转换 markdown 文件?

python-3.x - 在 Bokeh 中,如何向时间序列图表(悬停工具)添加工具提示?

python - 如何在 Google Trends 中点击 Load More 按钮并通过 Selenium 和 Python 打印所有标题

java - Selenium 获取快速变化元素的值 StaleElementReferenceException

java - Selenium 在查找第一个元素的前兄弟元素时永远挂起

python - 如何将 pandas DataFrame 的日期时间索引作为 isoDate 类型的字段插入 mongodb

python - app.asar(electron/nodejs)内没有这样的文件或目录

python - 返回数组中数字负值的函数

python - 如何创建 pandas 数据帧数组,其特定列的值始终大于特定列 - 通过使用 np.random.randint

python - 从两列计算和创建百分比列