Python请求使用列表

时间:2018-06-16 21:39:42

标签: python python-3.x python-requests

当我尝试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])

输出1:

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'>
[]

1 个答案:

答案 0 :(得分:0)

通过查看<class 'str'>

之前的输出值,您应该在来自文件的行尾添加换行符

尝试

a=content[1].strip()