scrapy 尝试在两个标记之间抓取内容

q3qa4bjr  于 2023-06-29  发布在  其他
关注(0)|答案(1)|浏览(111)

所以我试图抓取这个页面https://www.rxlist.com/abilify-drug.htm,我陷入了这个问题,我必须提取两个标签之间的内容。该页面的HTML代码如下:

<div class="monograph_cont"><h4>What Is Abilify?</h4>
<p>Abilify (aripiprazole) is a psychotropic drug (antipsychotic) that alters brain chemical activity used to treat:</p> 
<ul>
<li>schizophrenia,</li> 
<li>mania,</li> 
<li>depression,</li> 
<li>bipolar disorders,</li> 
<li>autistic disorder, and</li> 
<li>some irritable behavior disorders.</li> 
</ul>
<p>Generic Abilify is not available in the U.S., but is available in other countries under the name aripiprazole.</p>

<h4>What Are Side Effects of Abilify?</h4>
<p>Abilify may cause serious side effects including:</p>

<ul>
    <li>severe agitation or distress,</li>
    <li>feeling restless,</li>
    <li>twitching or uncontrollable movements of your eyes, lips, tongue, face, arms or legs,</li>
    <li>mask-like appearance of the face,</li>
    <li>trouble swallowing,</li>
    <li>speech problems,</li>
    <li>seizure (convulsions),</li>
    <li>thoughts of suicide or self-harm,</li>
    <li>stiff or rigid muscles,</li>
    <li>high fever,</li>
    <li>sweating,</li>
    <li>confusion,</li>
    <li>fast or uneven heartbeats,</li>
    <li>tremors (shaking),</li>
    <li>feeling lightheaded,</li>
    <li>chills,</li>
    <li>sore throat,</li>
    <li>mouth sores,</li>
    <li>skin sores,</li>
    <li>cough,</li>
    <li>difficulty breathing,</li>
    <li>increased thirst,</li>
    <li>increased urination,</li>
    <li>dry mouth, and</li>
    <li>fruity breath odor</li>
</ul>

<p>Get medical help right away, if you have any of the symptoms listed above.</p>
<p>Common side effects of Abilify include:</p>
<ul>
    <li>dizziness</li>
    <li>lightheadedness</li>
    <li>drowsiness</li>
    <li>weakness</li>
    <li>lightheadedness</li>
    <li>nausea</li>
    <li>vomiting</li>
    <li>stomach upset</li>
    <li>tiredness</li>
    <li>excess saliva or drooling</li>
    <li>choking or trouble swallowing</li>
    <li>blurred vision</li>
    <li>headache</li>
    <li>anxiety</li>
    <li>weight gain</li>
    <li>drowsiness</li>
    <li>sleep problems (insomnia)</li>
    <li>constipation</li>
</ul>
<p>Suicidal thoughts are may occur in some patients, especially children, teens, and young adults. Tell your doctor if this occurs. Other serious side effects of Abilify include:</p>
<ul>
    <li>tardive dyskinesia (involuntary, repetitive movements)</li>
    <li>neuroleptic malignant syndrome</li>
    <li>shaking (tremors)</li>
    <li>muscle spasms</li>
    <li>fainting</li>
    <li>mental/mood changes (such as increased anxiety, depression, suicidal thoughts)</li>
    <li>trouble swallowing</li>
    <li>restlessness (especially in the legs)</li>
    <li>mask-like expression of the face</li>
    <li>seizures</li>
    <li>signs of infection (such as fever, persistent sore throat) </li>
</ul>

<p>Seek medical care or call 911 at once if you have the following serious side effects:</p>
<ul>
<li>Serious eye symptoms such as sudden vision loss, blurred vision, tunnel vision, eye pain or swelling, or seeing halos around lights;</li>
<li>Serious heart symptoms such as fast, irregular, or pounding heartbeats; fluttering in your chest; shortness of breath; and sudden dizziness, lightheadedness, or passing out;</li>
<li>Severe headache, confusion, slurred speech, arm or leg weakness, trouble walking, loss of coordination, feeling unsteady, very stiff muscles, high fever, profuse sweating, or tremors.</li>
</ul>

<p>This document does not contain all possible side effects and others may occur. Check with your physician for additional information about side effects.</p>

<h4>Dosage for Abilify</h4>
<p>Abilify is available in tablet, orally disintegrating tablets, oral solution and injectable formulations. Dosage is variable and depends on multiple factors such as the ongoing mental problem, patient age, and other factors to be determined by the prescribing doctor.</p> 

<h4>Abilify In Children</h4>
<p>Abilify has been used in the pediatric population but such use should be discussed with a pediatric specialist.</p> 

<h4>What Drugs, Substances, or Supplements Interact with Abilify?</h4>
<p>Abilify may interact with other medicines that make you sleepy such as:</p> 
<ul>
<li>cold or allergy medicine,</li> 
<li>narcotic pain medicine,</li> 
<li>sleeping pills,</li> 
<li>muscle relaxers, and</li> 
<li>medicine for seizures, depression, or anxiety),</li> 
<li>medications to treat high blood pressure or a heart condition, 
</li><li>carbamazepine,</li> 
<li>phenobarbital,</li> 
<li>phenytoin,</li> 
<li>rifabutin,</li> 
<li>rifampin,</li> 
<li>ketoconazole,</li> 
<li>itraconazole,</li> 
<li>quinidine,</li> 
<li>fluoxetine,</li> 
<li>fluvoxamine, or</li> 
<li>paroxetine</li>
</ul> 

<p>Tell your doctor all medications and supplements you use.</p> 

<h4>Abilify During Pregnancy and Breastfeeding</h4>
<p>Benefits should outweigh risks in pregnant women. Women who are breastfeeding should not take Abilify.</p>

<h4>Additional Information</h4>
<p>Our Abilify Side Effects Drug Center provides a comprehensive view of available drug information on the potential side effects when taking this medication.
</p></div>

我试图从中提取<h4>What Are Side Effects of Abilify?</h4><h4>Dosage for Abilify</h4>之间的整个内容块
我试过这个response.css('#apPage > div > div.monograph_cont > h4:nth-child(5) ~ *:not(h4)::text').getall(),但它只给了我<p>标签的输出,而不是<li>元素,再加上这个返回所有<h4>Dosage for Abilify</h4>之后的文本,我不想要反正。
新来的人很困惑!

9lowa7mx

9lowa7mx1#

最优雅的解决方案可能是使用正则表达式来隔离目标部分,然后手动将其添加到scrapy.Selector并从中提取文本。
例如:

def parse(self, response):
    pattern = re.compile(r"(?<=of Abilify\?</h4>).*?(?=<h4>)", re.DOTALL)
    content = response.css("div.monograph_cont").re(pattern)
    yield {'text':scrapy.Selector(text=content[0]).xpath("//text()").getall())}

输出:

{'text': ['Abilify may cause serious side effects including:', '\r\n\r\n', '\r\n\t', 'severe agitation or distress,', '\r\n\t', 'feeling restless,', '\r\n\t', 'twitching or uncontrollable
movements of your eyes, lips, tongue, face, arms or legs,', '\r\n\t', 'mask-like appearance of the face,', '\r\n\t', 'trouble swallowing,', '\r\n\t', 'speech problems,', '\r\n\t', 'seizure
 (convulsions),', '\r\n\t', 'thoughts of suicide or self-harm,', '\r\n\t', 'stiff or rigid muscles,', '\r\n\t', 'high fever,', '\r\n\t', 'sweating,', '\r\n\t', 'confusion,', '\r\n\t', 'fas
t or uneven heartbeats,', '\r\n\t', 'tremors (shaking),', '\r\n\t', 'feeling lightheaded,', '\r\n\t', 'chills,', '\r\n\t', 'sore throat,', '\r\n\t', 'mouth sores,', '\r\n\t', 'skin sores,'
, '\r\n\t', 'cough,', '\r\n\t', 'difficulty breathing,', '\r\n\t', 'increased thirst,', '\r\n\t', 'increased urination,', '\r\n\t', 'dry mouth, and', '\r\n\t', 'fruity breath odor', '\r\n'
, '\r\n\r\n', 'Get medical help right away, if you have any of the symptoms listed above.', '\r\n', 'Common side effects of Abilify include:', '\r\n', '\r\n\t', 'dizziness', '\r\n\t', 'lig
htheadedness', '\r\n\t', 'drowsiness', '\r\n\t', 'weakness', '\r\n\t', 'lightheadedness', '\r\n\t', 'nausea', '\r\n\t', 'vomiting', '\r\n\t', 'stomach upset', '\r\n\t', 'tiredness', '\r\n\
t', 'excess saliva or drooling', '\r\n\t', 'choking or trouble swallowing', '\r\n\t', 'blurred vision', '\r\n\t', 'headache', '\r\n\t', 'anxiety', '\r\n\t', 'weight gain', '\r\n\t', 'drows
iness', '\r\n\t', 'sleep problems (insomnia)', '\r\n\t', 'constipation', '\r\n', '\r\n', 'Suicidal thoughts are may occur in some patients, especially children, teens, and young adults. Te
ll your doctor if this occurs. Other serious side effects of Abilify include:', '\r\n', '\r\n\t', 'tardive dyskinesia (involuntary, repetitive movements)', '\r\n\t', 'neuroleptic malignant
 syndrome', '\r\n\t', 'shaking (tremors)', '\r\n\t', 'muscle spasms', '\r\n\t', 'fainting', '\r\n\t', 'mental/mood changes (such as increased anxiety, depression, suicidal thoughts)', '\r\
n\t', 'trouble swallowing', '\r\n\t', 'restlessness (especially in the legs)', '\r\n\t', 'mask-like expression of the face', '\r\n\t', 'seizures', '\r\n\t', 'signs of infection (such as fe
ver, persistent sore throat) ', '\r\n', '\r\n\r\n', 'Seek medical care or call 911 at once if you have the following serious side effects:', '\r\n', '\r\n', 'Serious eye symptoms such as s
udden vision loss, blurred vision, tunnel vision, eye pain or swelling, or seeing halos around lights;', '\r\n', 'Serious heart symptoms such as fast, irregular, or pounding heartbeats; fl
uttering in your chest; shortness of breath; and sudden dizziness, lightheadedness, or passing out;', '\r\n', 'Severe headache, confusion, slurred speech, arm or leg weakness, trouble walk
ing, loss of coordination, feeling unsteady, very stiff muscles, high fever, profuse sweating, or tremors.', '\r\n', '\r\n\r\n', 'This document does not contain all possible side effects a
nd others may occur. Check with your physician for additional information about side effects.']}

相关问题