Get element by class requests_html

Viewed 252

I'm trying to scrape Quora and get the text inside an element by it's CSS class. For example, the HTML around the question looks like this:

<p class="q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start" style="box-sizing: border-box; margin-bottom: 1em; overflow-wrap: anywhere; direction: ltr;">
<span style="font-weight: normal; font-style: italic; background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%;">
Thanks for A2A!
</span>
</p>

How could I scrape the "Thanks for the A2A" text inside of this? This is what I currently have:

from requests_html import HTMLSession


url = 'https://www.quora.com/What-is-the-best-case-of-You-just-picked-a-fight-with-the-wrong-person-that-youve-witnessed'

r = session.get(url)

r.html.render(sleep=1, keep_page=True, scrolldown=1)

videos = r.html.find('#q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start')

print(videos.text)
2 Answers

You should try:

videos = r.html.find('.q-text.qu-display--block.qu-wordBreak--break-word.qu-textAlign--start')

instead of

videos = r.html.find('#q-text qu-display--block qu-wordBreak--break-word qu-textAlign--start')

You can read more about multiple classes selector here.

You are confusing id with class selector.

This is working sample of your code.

from requests_html import HTMLSession


session = HTMLSession()

url = 'https://www.quora.com/What-is-the-best-case-of-You-just-picked-a-fight-with-the-wrong-person-that-youve-witnessed'
r = session.get(url)
r.html.render(sleep=1, keep_page=True, scrolldown=1)
videos = r.html.find('.q-text.qu-display--block.qu-wordBreak--break-word.qu-textAlign--start')
print(videos)
Related