Мой веб-код с beautifulsoup не проходит дальше первой страницы

#python #web-scraping #beautifulsoup

#python #веб-очистка #beautifulsoup

Вопрос:

похоже, что он не проходит дальше первой страницы. Что не так? Также, если слово, которое вы ищете, находится в ссылке, оно не будет предоставлять правильные вхождения, оно отобразит 5 выходных данных с 5 в качестве вхождения

 import requests from bs4 import BeautifulSoup 

for i in range (1,5):

    url = 'https://www.nairaland.com/search/ipob/0/0/0/{}'.format(i)
    the_word = 'is' 
    r = requests.get(url, allow_redirects=False)
    soup = BeautifulSoup(r.content, 'lxml')
    words = soup.find(text=lambda text: text and the_word in text) 
    print(words) 
    count =  len(words)
    print('nUrl: {}ncontains {} occurrences of word: {}'.format(url, count, the_word))
  

Комментарии:

1. при правильном коде с отступом это происходит. Я вижу 4 набора результатов.

2. измените свой for цикл на for i in range (5):

3. Вам просто нужно перевести from bs4 import BeautifulSoup в новую строку и изменить цикл for на for i in range (6): .

Ответ №1:

Если вы хотите пройти первые 6 страниц, измените диапазон в своем цикле:

 for i in range (6):   # the first page is addressed at index `0`
  

или:

 for i in range (0,6):
  

вместо:

 for i in range (1,5):    # this will start from the second page, since the second page is indexed at `1`
  

Комментарии:

1. Затем измените диапазон for i in range (6):

2. Я хочу получить вхождения на первых 6 страницах. Он отображает события только для первой страницы, несмотря на изменение на range (6)

3. Это дает мне результат

Ответ №2:

Попробуйте:

 import requests
from bs4 import BeautifulSoup 

for i in range(6):
    url = 'https://www.nairaland.com/search/ipob/0/0/0/{}'.format(i)
    the_word = 'afonja' 
    r = requests.get(url, allow_redirects=False)
    soup = BeautifulSoup(r.content, 'lxml')
    words = soup.find(text=lambda text: text and the_word in text) 
    print(words)
    count = 0
    if words:
        count = len(words)
    print('nUrl: {}ncontains {} occurrences of word: {}'.format(url, count, the_word))
  

РЕДАКТИРОВАТЬ после новых спецификаций.

Предполагая, что слово для подсчета такое же, как в URL, вы можете отметить, что слово выделено на странице и распознается span class=highlight в html.

Итак, вы можете использовать этот код:

 import requests
from bs4 import BeautifulSoup 

for i in range(6):
    url = 'https://www.nairaland.com/search/afonja/0/0/0/{}'.format(i)
    the_word = 'afonja' 
    r = requests.get(url, allow_redirects=False)
    soup = BeautifulSoup(r.content, 'lxml')
    count = len(soup.find_all('span', {'class':'highlight'})) 
    print('nUrl: {}ncontains {} occurrences of word: {}'.format(url, count, the_word))
  

и вы получаете:

 Url: https://www.nairaland.com/search/afonja/0/0/0/0
contains 30 occurrences of word: afonja

Url: https://www.nairaland.com/search/afonja/0/0/0/1
contains 31 occurrences of word: afonja

Url: https://www.nairaland.com/search/afonja/0/0/0/2
contains 36 occurrences of word: afonja

Url: https://www.nairaland.com/search/afonja/0/0/0/3
contains 30 occurrences of word: afonja

Url: https://www.nairaland.com/search/afonja/0/0/0/4
contains 45 occurrences of word: afonja

Url: https://www.nairaland.com/search/afonja/0/0/0/5
contains 50 occurrences of word: afonja
  

Комментарии:

1. Если слово afonja является частью поисковой ссылки, оно отобразит правильные вхождения слова

2. Чтобы понять вашу проблему, вы должны уточнить: 1) какой URL вы хотите очистить 2) какое слово вы хотите найти в тексте страницы 3) какую проблему вы получаете

3. Я хочу очистить 5 страниц по этому URL nairaland.com/search ? q = afonjaamp;board = 0amp; topicsonly = 2 для слова afonja . Вхождения / подсчеты afonja неверны

4. Является ли имя слова для подсчета таким же, как слово в URL?

5. Да, это есть в URL.

Ответ №3:

Для меня это работает нормально:

 import requests
from bs4 import BeautifulSoup

if __name__ == "__main__":

    # correct the range, 0, 6 to go from first page to the fifth one (starting counting from "0")
    # or try 0, 5 to go from 0 to 5 (five pages in total)
    for i in range(0, 6): # range(0, 4)

        url = 'https://www.nairaland.com/search/ipob/0/0/0/{}'.format(i)
        print(url, "url")
        the_word = 'is'
        r = requests.get(url, allow_redirects=False)
        soup = BeautifulSoup(r.content, 'lxml')
        words = soup.find(text=lambda text: text and the_word in text)
        print(words)
        count =  len(words)
        print('nUrl: {}ncontains {} occurrences of word: {}'.format(url, count, the_word))
  

Это результат:

 https://www.nairaland.com/search/ipob/0/0/0/0 url
 is somewhere in Europe sending semi nude video on the internet.Are you proud of such groups with such leader?

Url: https://www.nairaland.com/search/ipob/0/0/0/0
contains 110 occurrences of word: is
https://www.nairaland.com/search/ipob/0/0/0/1 url
Notre is a French word; means 'Our"...and Dame means "Lady" So Notre Dame means Our Lady.

Url: https://www.nairaland.com/search/ipob/0/0/0/1
contains 89 occurrences of word: is
https://www.nairaland.com/search/ipob/0/0/0/2 url
How does all this uselessness Help Foolish 

Url: https://www.nairaland.com/search/ipob/0/0/0/2
contains 43 occurrences of word: is
https://www.nairaland.com/search/ipob/0/0/0/3 url
Dumb fuckers everywhere. I thought I was finally going to meet someone that has juju and can show me. Instead I got a hopeless broke buffoon that loves boasting online. Nairaland I apologize on the behalf of this waste of space and time. He is not even worth half of the data I have spent writing this post. 

Url: https://www.nairaland.com/search/ipob/0/0/0/3
contains 308 occurrences of word: is
https://www.nairaland.com/search/ipob/0/0/0/4 url
People like FFK, Reno, Fayose etc have not been touched, it is an unknown prophet that hasn't said anything against the FG that you expect the FG to waste its time on. 

Url: https://www.nairaland.com/search/ipob/0/0/0/4
contains 168 occurrences of word: is
https://www.nairaland.com/search/ipob/0/0/0/5 url
 children send them to prison

Url: https://www.nairaland.com/search/ipob/0/0/0/5
contains 29 occurrences of word: is

Process finished with exit code 0
  

Комментарии:

1. Я понял проблему, если слово, которое вы ищете, находится в ссылке, оно не будет отображать правильное results.is это ошибка?

2. Я думаю, это из-за параметра «text =». Если вы хотите что-то найти также внутри ссылки, тогда вам нужно найти тег «a», извлекая «href»

3. Вы пытались найти нужное слово «жестоко» через регулярное выражение? Если вы используете requests-html, легко получить готовый к анализу объект, подобный html. Затем вы можете искать вхождения с помощью регулярного выражения.

4. Чтобы найти слово с регулярным выражением: импортируйте повторно , all_result = re.findall(повторно.compile(«ваша строка»), text_var). All_result — это список. Вы можете использовать элементы внутри и получить размер с помощью Len()

Ответ №4:

Кроме того, поисковое слово имеет свое собственное имя класса, поэтому вы можете просто посчитать их. Приведенное ниже значение корректно возвращает то, что не найдено на странице. Вы могли бы использовать этот подход в своем цикле.

 import requests 
from bs4 import BeautifulSoup as bs

r = requests.get('https://www.nairaland.com/search?q=afonjaamp;board=0amp;topicsonly=2')
soup = bs(r.content, 'lxml')
occurrences = len(soup.select('.highlight'))
print(occurrences)
  

 import requests 
from bs4 import BeautifulSoup as bs

for i in range(9):
    r = requests.get('https://www.nairaland.com/search/afonja/0/0/0/{}'.format(i))
    soup = bs(r.content, 'lxml')
    occurrences = len(soup.select('.highlight'))
    print(occurrences)