I made a parser, you need it to compare ID dates every 30 minutes for parsing new content. But I can't use the "-" sign, what should I do?
The problem is date-time=True
import telebot
import requests
import time
from bs4 import BeautifulSoup
URL = "****"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
post = soup.find("div", class_="largeTitle", date-time=True)
post_id = post["date=time"]
CodePudding user response:
Use the attributes
argument to specify the attributes in a dictionary, rather than named arguments, when the attribute name isn't a valid Python name.
post = soup.find("div", attributes = {"class": "largeTitle", "date-time": True})
CodePudding user response:
You can use CSS selector as well "div.largeTitle[date-time]"
:
from bs4 import BeautifulSoup
html = """\
<div date-time="xxx">This I want</div>
<div >This I don't</div>
"""
soup = BeautifulSoup(html, "html.parser")
post = soup.select_one("div.largeTitle[date-time]")
print(post)
Prints:
<div date-time="xxx">This I want</div>
CodePudding user response:
this looks minus operator, replace it with underscore _ or check this link:
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find