【问题标题】:NoneType object is not callable | Python Printing regex [duplicate]NoneType 对象不可调用 | Python打印正则表达式[重复]
【发布时间】:2018-04-28 16:01:49
【问题描述】:

我目前正在尝试清理从 twitter 上抓取的一些数据,但是当我尝试打印出stamp_print 变量(只是一个简单的时间戳。)我不太确定我做错了什么。

import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re  

my_url = "https://twitter.com/realDonaldTrump?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()


page_soup = soup(page_html, "html.parser")

tweets = page_soup.findAll("p",{"class":"TweetTextSize TweetTextSize--normal js-tweet-text tweet-text"})
time_stamp = page_soup.find_all("a",{"class":"tweet-timestamp"})

i = 0
for tweet in tweets:
    stamp = time_stamp[i]
    stamp_print = stamp.match('[0-9]+\s\w+|[0-9]+\sw+')
    print(stamp_print.get_text)
    i+=1
    print('---------------------------------------------------------------------')
    print(tweet.get_text())
    print('\n')

【问题讨论】:

  • stamp = time_stamp[i] 之后尝试print(re.findall(r'\d+\s*\w+', stamp.get_text))。注意stamp 没有match 方法。
  • 确保 stamp_print 不是 None
  • 您的正则表达式不会产生您想要的结果。您希望提取时间戳的哪一部分?

标签: regex python-3.x beautifulsoup


【解决方案1】:

这最终成功了,谢谢。

import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re  

my_url = "https://twitter.com/realDonaldTrump?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()


page_soup = soup(page_html, "html.parser")

tweets = page_soup.findAll("p",{"class":"TweetTextSize TweetTextSize--normal js-tweet-text tweet-text"})
time_stamp = page_soup.find_all("a",{"class":"tweet-timestamp"})

i = 0
for tweet in tweets:
    stamp = time_stamp[i]
    print(re.findall(r'[0-9]+\s\w+|\w+\s[0-9]+', stamp.get_text())) 
    i+=1
    print('---------------------------------------------------------------------')
    print(tweet.get_text())
    print('\n')

【讨论】:

    猜你喜欢
    • 2015-08-22
    • 1970-01-01
    • 2021-01-12
    • 2016-11-03
    • 2014-06-21
    • 2019-10-20
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多