Python BeautifulSoup find_all()在for循环中不起作用

时间:2016-12-16 01:31:07

标签: python

import requests, os
from bs4 import BeautifulSoup


directory = os.path.normpath("C:\\Users\x\\Desktop\\names.txt")
f = open(directory, 'r', encoding='utf8')
x = f.readlines()
for line in x:
    s = line.split(",")[0].replace(" ","")
    n = line.split(",")[1].replace(" ","")
    print(s + " " + n)

    res = requests.get('http://www.scopus.com/results/authorNamesList.uri?    origin=searchauthorlookup&src=al&edit=&poppUp=&basicTab=&affiliationTab=&advancedTab=&st1=' + s + '&st2=' + n + '&institute=&orcidId=&authSubject=LFSC&_authSubject=on&authSubject=HLSC&_authSubject=on&authSubject=PHSC&_authSubject=on&authSubject=SOSC&_authSubject=on&s=AUTH--LAST--NAME%28' + s + '%29+AND+AUTH--FIRST%28' + n + '%29&sdt=al&sot=al&searchId=81BECD653C7453F7BFBBB1CD8D8E5B04.wsnAw8kcdt7IPYLO0V48gA%3A220&exactSearch=off&sid=81BECD653C7453F7BFBBB1CD8D8E5B04.wsnAw8kcdt7IPYLO0V48gA%3A220')   
    content = res.content    
    soup = BeautifulSoup(content, 'lxml').find_all('div', {'class':'dataCol2'})
    for item in soup:
        print(item.text)
        break   

beautifulsoup。(内容,' lxml')。find_all仅适用于x中的最后一行。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

它按预期工作:

import requests, os
from bs4 import BeautifulSoup


x=["Aaroe, L","Abbas, Z","Abbott, G"]

for line in x:
    l=line.split(',')
    s=l[0].strip()
    n=l[1].strip()
    print("------------------------------------")
    print("FirstName: "+s + ", Last Name: " + n)
    res = requests.get('http://www.scopus.com/results/authorNamesList.uri?origin=searchauthorlookup&src=al&edit=&poppUp=&basicTab=&affiliationTab=&advancedTab=&st1=' + s + '&st2=' + n + '&institute=&orcidId=&authSubject=LFSC&_authSubject=on&authSubject=HLSC&_authSubject=on&authSubject=PHSC&_authSubject=on&authSubject=SOSC&_authSubject=on&s=AUTH--LAST--NAME%28' + s + '%29+AND+AUTH--FIRST%28' + n + '%29&sdt=al&sot=al&searchId=81BECD653C7453F7BFBBB1CD8D8E5B04.wsnAw8kcdt7IPYLO0V48gA%3A220&exactSearch=off&sid=81BECD653C7453F7BFBBB1CD8D8E5B04.wsnAw8kcdt7IPYLO0V48gA%3A220')   
    content = res.content    
    soup = BeautifulSoup(content, 'lxml').find_all('div', {'class':'dataCol2'})
    for item in soup:
        print(item.text.strip())
        break

输出:

------------------------------------
FirstName: Aaroe, Last Name: L
Aarøe, Lene



Aaroe, Lene
------------------------------------
FirstName: Abbas, Last Name: Z
Abbas, Zaigham



Abbas, Z.


ABBAS, ZAIGHAM
------------------------------------
FirstName: Abbott, Last Name: G
Abbott, Albert Glenn



Abbott, A. G.


Abbott, Albert G.


Abbott, A.