首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python-使用列表中的字符串请求GET

Python-使用列表中的字符串请求GET
EN

Stack Overflow用户
提问于 2018-06-17 05:39:42
回答 1查看 36关注 0票数 1

当我尝试GET一个有请求的网页时,我成功地得到了页面,而链接却存储在一个str变量中。然而,当我尝试获取一个字符串数组的元素时,我无法检索到页面。

输入1:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)

Output2:

代码语言:javascript
复制
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'>
[]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-17 05:47:15

通过查看<class 'str'>之前的输出值,您应该在文件的行尾有一个换行符

尝试使用

代码语言:javascript
复制
a=content[1].strip()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50891971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档