1

Im using python 3.8x to try and scrape a randomly generated sentence from this website. https://randomwordgenerator.com/sentence.php Except when I read it, the generated sentence isn't in the HTML. Can any one help me find a way to scrape the generated sentence? I found the HTML tags when the sentence is generated but it doesn't generate when I request.

Here is my code.

random_sentence_webpage = 'https://randomwordgenerator.com/sentence.php'

# The HTML tag for the generated sentence
start_marker = '"support-sentence">'
end_marker = '</span>'

from urllib.request import urlopen, Request
headers = {'User-Agent': 'Chrome/81.0.4044.129'}
reg_url = random_sentence_webpage
req = Request(url=reg_url, headers=headers) 
html = urlopen(req).read()
html_text = html.decode('utf-8', 'backslashreplace')

starting_position = html_text.find(start_marker)
end_position = html_text.find(end_marker,starting_position)

random_generated_sentence = html_text[starting_position + len(start_marker):end_position]

print(random_generated_sentence)
JamesC89
  • 13
  • 3
  • most likely the sentence is generate in the javascript code, so it's not in the initial html. when you do a request like you did, you don't execute the javascript just get the plain html as response – Bernardo stearns reisen Apr 29 '20 at 01:22

2 Answers2

1

You will find more details here Using python Requests with js pages

but the short solution is using requests_html:

from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://randomwordgenerator.com/sentence.php')
r.html.render() 
print(r.html.find(".support-sentence")[0].text)

outputs

Having no hair made him look even hairier.
0

When I run your code I get a blob of html output.

</div>
</div>
<div class="container pt bottom_desc">
<div class="row">
<div class="col-md-6">
<p>If you're visiting this page, you're likely here because you're searching for a random sentence. Sometimes a random word just isn't enough, and that is where the random sentence generator comes into play. By inputting the desired number, you can make a list of as many random sentences as you want or need. Producing random sentences can be helpful in a number of different ways.</p>
<p>For writers, a random sentence can help them get their creative juices flowing. Since the topic of the sentence is completely unknown, it forces the writer to be creative when the sentence appears. There are a number of different ways a writer can use the random sentence for creativity. The most common way to use the sentence is to begin a story. Another option is to include it somewhere in the story. A much more difficult challenge is to use it to end a story. In any of these cases, it forces the writer to think creatively since they have no idea what sentence will appear from the tool.</p>
<p>For those writers who have writers' block, this can be an excellent way to take a step to crumbling those walls. By taking the writer away from the subject matter that is causing the block, a random sentence may allow them to see the project they're working on in a different light and perspective. Sometimes all it takes is to get that first sentence down to help break the block.</p>
<p>It can also be successfully used as a daily exercise to get writers to begin writing. Being shown a random sentence and using it to complete a paragraph each day can be an excellent way to begin any writing session.</p>
<p>Random sentences can also spur creativity in other types of projects being done. If you are trying to come up with a new concept, a new idea or a new product, a random sentence may help you find unique qualities you may not have considered. Trying to incorporate the sentence into your project can help you look at it in different and unexpected ways than you would normally on your own.</p>
<p>It can also be a fun way to surprise others. You might choose to share a random sentence on social media just to see what type of reaction it garners from others. It's an unexpected move that might create more conversation than a typical post or tweet.</p>
<p>These are just a few ways that one might use the random sentence generator for their benefit. If you're not sure if it will help in the way you want, the best course of action is to try it and see. Have several random sentences generated and you'll soon be able to see if they can help with your project.</p>
<p>Our goal is to make this tool as useful as possible. For anyone who uses this tool and comes up with a way we can improve it, we'd love to know your thoughts. Please contact us so we can consider adding your ideas to make the random sentence generator the best it can be.</p>
<div class="faq" id="faq" itemscope="" itemtype="https://schema.org/FAQPage"><h2 style="margin-bottom:25px">Frequently Asked Questions</h2>
<div itemscope="" itemprop="mainEntity" itemtype="https://schema.org/Question">
<h3 class="faq__title" itemprop="name">Are random sentences computer generated?</h3>
<div itemscope="" itemprop="acceptedAnswer" itemtype="https://schema.org/Answer">
<div itemprop="text"><p>No, the random sentences in our generator are not computer generated. We considered using computer generated sentences when building this tool, but found the results to be disappointing. Even though it took a lot of time, all the sentences in this generator were created by us.</p></div>
</div>
</div>
<div itemscope="" itemprop="mainEntity" itemtype="https://schema.org/Question">
<h3 class="faq__title" itemprop="name">Can I use these random sentences for my project?</h3>
<div itemscope="" itemprop="acceptedAnswer" itemtype="https://schema.org/Answer">
<div itemprop="text"><p>Yes! Feel free to use any of the random sentences for any project that you may be doing.</p></div>
</div>
</div>

I guess you want to extract these p tags and random texts.

You can use beautifulsoup to format your output. When I run your code I get many random text strings embedded in

tags. You need to find the correct paths to find those p tags and get the text.

This is a demo. You need to change it based on your needs.

random_sentence_webpage = 'https://randomwordgenerator.com/sentence.php'

# The HTML tag for the generated sentence
start_marker = '"support-sentence">'
end_marker = '</span>'

from urllib.request import urlopen, Request
headers = {'User-Agent': 'Chrome/81.0.4044.129'}
reg_url = random_sentence_webpage
req = Request(url=reg_url, headers=headers) 
html = urlopen(req).read()
html_text = html.decode('utf-8', 'backslashreplace')
starting_position = html_text.find(start_marker)
end_position = html_text.find(end_marker,starting_position)

random_generated_sentence = html_text[starting_position + len(start_marker):end_position]

# print(random_generated_sentence)

from bs4 import BeautifulSoup


soup = BeautifulSoup (random_generated_sentence, features="lxml")

block_ps =  soup.findAll("div", {"class": "col-md-6"})
for a in block_ps:
  print(a.findAll('p'))
Arl N
  • 1
  • 3