Python-запросы GET со строками из списка

1

Когда я пытаюсь GET веб-страницу с запросами, я получаю страницу успешно, пока ссылка хранится в переменной str. Тем не менее, когда я пытаюсь получить элемент из массива str, я не могу получить страницу.

Вход 1:

import requests
from bs4 import BeautifulSoup
import re

f = open("pages.txt","r")
file = open("parsed.txt","a")
content = f.readlines()

for i in range(1):

    a="http://registration.boun.edu.tr/scripts/sch.asp?donem=2017/2018-3&kisaadi=BM&bolum=BIOMEDICAL+ENGINEERING"
    print(a + " " + str(type(a) ) )

    req_link=a
    r=requests.get(req_link)
    c=r.content

    soup=BeautifulSoup(c,"html.parser")
    all=soup.find_all("td")
    print(all[38])

Output1:

PS E:\pythonCodes\BounCP> python .\getClasses.py
http://registration.boun.edu.tr/scripts/sch.asp?donem=2017/2018-3&kisaadi=BM&bolum=BIOMEDICAL+ENGINEERING <class 'str'>
<td><font style="font-size:12px">BM  519.01</font> </td>

Вход 2:

import requests
from bs4 import BeautifulSoup
import re

f = open("pages.txt","r")
file = open("parsed.txt","a")
content = f.readlines()

for i in range(1):

    a=content[1]
    print( content[1] + " "+ str(type(content[1]) ) )

    req_link=a
    r=requests.get(req_link)
    c=r.content

    soup=BeautifulSoup(c,"html.parser")
    all=soup.find_all("td")
    #all=all[38:]
    print(all)

Выход2:

PS E:\pythonCodes\BounCP> python .\getClasses.py
http://registration.boun.edu.tr/scripts/sch.asp?donem=2017/2018-3&kisaadi=BM&bolum=BIOMEDICAL+ENGINEERING
 <class 'str'>
[]
Теги:
python-requests
python-3.x

1 ответ

0
Лучший ответ

У вас должна быть строка в конце строки, исходящей из файла, путем просмотра выходного значения до <class 'str'>

попробуйте

a=content[1].strip()
  • 0
    Работает как шарм. Спасибо.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню