将文本转化为Python列表。

huangapple go评论59阅读模式
英文:

transform lines into a list with python

问题

以下是翻译好的代码部分:

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox(executable_path=r'C:/path/geckodriver.exe')
browser.get('https://brainly.com.br/app/ask?entry=hero&q=jhyhv+vjh')

html = browser.execute_script("return document.documentElement.outerHTML")
p = []
soup = BeautifulSoup(html, 'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref = link.get('href')
    rt = ('https://brainly.com.br' + str(ref))
    p.append(rt)
    print(p)

# Obs:Expected Exit:
# ['https://link1', 'https://link2']

希望这有所帮助。

英文:

Developing my algorithm, which is below:

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


browser =webdriver.Firefox(executable_path=r'C:/path/geckodriver.exe')
browser.get('https://brainly.com.br/app/ask?entry=hero&q=jhyhv+vjh')

html = browser.execute_script("return document.documentElement.outerHTML")
p=[]
soup=BeautifulSoup(html,'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref=link.get('href')
    rt = ('https://brainly.com.br'+str(ref))
    ar = p.append(rt)
    print(ar) 

Everything goes well, with a slight exception. When trying to execute the algorithm without using *append* to create the list, it works normally, but when using it, I get an Exit None.

My Doubt and What I Need to Change To Have a Valid and Orderly Exit in a List !.

Obs:Expected Exit:

['https://link1', 'https://link2']

答案1

得分: 0

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox(executable_path=r'C:/path/geckodriver.exe')
browser.get('https://brainly.com.br/app/ask?entry=hero&q=jhyhv+vjh')

html = browser.execute_script("return document.documentElement.outerHTML")
p = []
soup = BeautifulSoup(html, 'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref = link.get('href')
    rt = ('https://brainly.com.br' + str(ref))
    p.append(rt)
print(p)
英文:
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


browser =webdriver.Firefox(executable_path=r'C:/path/geckodriver.exe')
browser.get('https://brainly.com.br/app/ask?entry=hero&q=jhyhv+vjh')

html = browser.execute_script("return document.documentElement.outerHTML")
p=[]
soup=BeautifulSoup(html,'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref=link.get('href')
    rt = ('https://brainly.com.br'+str(ref))
    p.append(rt)
print(p) 

答案2

得分: 0

append会直接修改列表,所以具有结果的变量是p

# (...)

p = []
soup = BeautifulSoup(html, 'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref = link.get('href')
    rt = ('https://brainly.com.br' + str(ref))
    p.append(rt)
print(p)
英文:

append modifies the list in place, so the variable that has the result is p.

# (...)

p=[]
soup=BeautifulSoup(html,'html.parser')
for link in soup.select('div > a[href*=""]'):
    ref=link.get('href')
    rt = ('https://brainly.com.br'+str(ref))
    p.append(rt)
print(p)

huangapple
  • 本文由 发表于 2020年1月6日 23:58:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定