简体   繁体   English

Python请求GET与列表中的字符串

[英]Python-requests GET with strings from a list

When I try to GET a webpage with requests , I get the page successfully while the link stored in a str variable. 当我尝试GET请求一个网页,我顺利拿到了页面,同时存储在str变量的链接。 Yet, when I try to get with an element of a str array I can't retrieve the page. 但是,当我尝试获取str数组的元素时,无法检索该页面。

Input 1: 输入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: 输出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>

Input 2: 输入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)

Output2: 输出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'>
[]

You should have a linebreak at the end of your line coming from the file, by seeing the output value before <class 'str'> 通过查看<class 'str'>之前的输出值,您应该在行末尾来自文件的换行

try with 尝试

a=content[1].strip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM