T
T
TechNOIR2018-02-08 22:32:30
Python
TechNOIR, 2018-02-08 22:32:30

Python. How to check if the result contains a word?

Good afternoon.
There is the following code:

spoiler
import requesocks
from stem import Signal
from stem.control import Controller
from bs4 import BeautifulSoup
from urllib2 import urlopen

def change_proxy():
    with Controller.from_port(port = 9151) as controller:
            controller.authenticate(password="password")
            controller.signal(Signal.NEWNYM)

session = requesocks.session()
session.proxies = {'http': 'socks5://127.0.0.1:9150',
'https': 'socks5://127.0.0.1:9150'}
link = "https://www.reformagkh.ru/myhouse/profile/view/7698523/"
res = session.get(link).text
soup = BeautifulSoup(''.join(res), 'html.parser')

if 'center' in soup.findAll('script')[14]:
    print "1"
else:
    print "2"
change_proxy()

In it, at the end, there is a check for the presence of the word center as a result of issuing soup.findAll('script')[14]
The result of issuing soup.findAll('script')[14]:
spoiler
<script type="text/javascript">
    window.onload = function () {
        var mapConfig = {
            center: [48.720853,44.473506],
            zoom: 14
        };

        ymaps.ready(function () {
            var rgkhMap = new ymaps.Map('map', mapConfig),
                balloonContentBody = "\u043e\u0431\u043b. \u0412\u043e\u043b\u0433\u043e\u0433\u0440\u0430\u0434\u0441\u043a\u0430\u044f, \u0433. \u0412\u043e\u043b\u0433\u043e\u0433\u0440\u0430\u0434",
                balloonContentHeader = "\u0410\u043d\u0433\u0430\u0440\u0441\u043a\u0430\u044f, 59, \u0441\u043e\u043e\u0440\u0443\u0436\u0435\u043d\u0438\u0435 ",
                balloonContent = '<h3>' + balloonContentHeader + '</h3><p>' + balloonContentBody + '</p>';

            rgkhMap.controls.remove('mapTools')
                    .remove('geolocationControl')
                    .remove('searchControl')
                    .remove('trafficControl')
                    .remove('rulerControl');

                            var myPlacemark = new ymaps.Placemark(
                    [48.720853,44.473506],
                    {balloonContent: balloonContent}
                );
                rgkhMap.geoObjects.add(myPlacemark);
                    });
    }
</script>

As you can see from the output result, the word center is present.
But the code to check for the existence returns a response of 2, i.e. does not contain :(
Maybe somehow I'm not doing the check correctly?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-02-08
@dimonchik2013

if  soup.findAll('script')[14].find('center') != -1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question