이봐, 나는 개행에서 목록을 제거하려고하는데 내가 얻는 출력은 비어 있습니다. 내가 도대체 뭘 잘못하고있는 겁니까? 나는 그것을 실행하기 위해 jupyter를 사용하고 있습니다.
url = 'https://en.wikipedia.org/wiki/Microsoft_3D_Viewer'
paragraphs = []
titles = []
scraped_content = []
scraped_titles = []
scraped_list = []
response = requests.get(url, time.sleep(2))
soup2 = BeautifulSoup(response.content, "html.parser")
paragraphs = soup2.find_all('p')
lists = soup2.find_all('ul')
titles = soup2.find_all(re.compile('^h[1-4]$'))
for paragraph in paragraphs:
paragraphs = [paragraph.text]
paragraphs = paragraph.get_text()
scraped_content.append(paragraphs)
for title in titles:
titles = [title.text]
titles = title.get_text()
scraped_titles.append(titles)
scraped_content = list(map(str.strip, scraped_content))
scraped_content
귀하의 코드는에 대한 매개 변수를 제외하고는 작동하는 것처럼 보입니다 requests.get
.
requests.get
는 불필요하고 문제를 일으킬 수 있으므로 제거합니다 . 2 초의 제한 시간을 추가하려는 경우 문서화 된대로timeout=2
대신 사용하십시오 .paragraphs
및 titles
다음 즉시 재 할당. 목록 이해를 사용하여 결과를보다 직접적으로 얻을 수 있습니다.from bs4 import BeautifulSoup
import requests
import time
import re
url = 'https://en.wikipedia.org/wiki/Microsoft_3D_Viewer'
response = requests.get(url)
paragraphs = []
titles = []
scraped_content = []
scraped_titles = []
scraped_list = []
soup2 = BeautifulSoup(response.content, "html.parser")
paragraphs = soup2.find_all('p')
lists = soup2.find_all('ul')
titles = soup2.find_all(re.compile('^h[1-4]$'))
scraped_content = [paragraph.get_text() for paragraph in paragraphs]
scraped_titles = [title.get_text() for title in titles]
trimmed_content = [content.strip() for content in scraped_content]
trimmed_content
출력 (잘림, 첫 번째 줄만 표시됨) :
['3D Viewer (formerly Mixed Reality Viewer and before that, View 3D)[2][3][4] is a 3D object viewer and Augmented Reality application that was first included in Windows 10 1703. It supports the .fbx, .3mf, .obj, and .stl and many more file formats[5] listed in features section.',
...
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다